Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need understanding for public and private.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Need understanding for public and private

    I have pasted the code, now my query is that in 1st class named "accesssp" I have added object and then written SOP then why cant I have output as value of B ?? b's access is private but I am fetching value in same class

    I've done section BOLD which I don't understand.



    class accesssp {

    public int a=56;
    private int b=5566;
    public int c=58766;

    accesssp sp2 = new accesssp();
    System.out.println(b);


    }

    class accesssp1 extends accesssp{

    public void accessd() {

    System.out.println(a);
    System.out.println(c);

    }

    }
    public class Access_Spf {

    public static void main(String[] args) {

    accesssp1 sp1 = new accesssp1();
    sp1.accessd();
    }

    }

    I have pasted the code, now my query is that in 1st class named "accesssp" I have added object and then written SOP then why cant I have output as value of B ?? b's access is private but I am fetching value in same class

    I've done section BOLD which I don't understand.



    class accesssp {

    public int a=56;
    private int b=5566;
    public int c=58766;

    accesssp sp2 = new accesssp();
    System.out.println(b);


    }

    class accesssp1 extends accesssp{

    public void accessd() {

    System.out.println(a);
    System.out.println(c);

    }

    }
    public class Access_Spf {

    public static void main(String[] args) {

    accesssp1 sp1 = new accesssp1();
    sp1.accessd();
    }

    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need understanding for public and private.

    Post full text of any error messages.

    Move statement into a method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Chatting application in java (PUblic And private)
    By nakul dev in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 27th, 2012, 12:27 PM
  2. Public class help/error
    By Plural in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 11th, 2010, 05:22 PM
  3. Convert string to private and public key
    By Ganezan in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 5th, 2010, 08:37 AM
  4. Private or public variables??
    By igniteflow in forum Java Theory & Questions
    Replies: 2
    Last Post: September 17th, 2009, 08:07 AM
  5. [SOLVED] Difference between public and private variable and their uses
    By napenthia in forum Java Theory & Questions
    Replies: 1
    Last Post: April 22nd, 2009, 11:36 AM

Tags for this Thread