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: Whats wrong with Java's Private Membership

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Whats wrong with Java's Private Membership

    The code is:
    class room
    {
        float z;
        float y;
    }
        public static void main()
        {
            float area;
            room r = new room();
            r.z=10;
            r.y=20;
            area=r.z*r.y;
            System.out.println("area = ", area);
    }
    Why is this giving me an answer area = 200
    I know that by default member's are private so I should not be able to access the variables without creating an member function that is public.
    Last edited by helloworld922; August 25th, 2010 at 04:33 PM.


  2. #2
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: Whats wrong with Java's Private Membership

    Quote Originally Posted by CandorZ View Post
    I know that by default member's are private so I should not be able to access the variables without creating an member function that is public.

    no, the default access means the the element is visible/accessible inside the same package. so, because the class room is in the same package like the main, also the variable x and z are visible. to be precise, the visibility of default is

    from the same class: yes
    from any class in the same package: yes
    from a subclass in the same package: yes
    from a subclass outside the same package: no
    from any non-subclass class outside the package: no
    Last edited by j2me64; August 25th, 2010 at 06:48 AM.

Similar Threads

  1. Whats wrong with my looping?
    By argie123sky in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 21st, 2010, 09:13 PM
  2. whats wrong with this one....
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 6th, 2009, 11:08 AM
  3. help whats wrong
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 3rd, 2009, 02:41 AM
  4. [SOLVED] whats wrong with my IDE
    By chronoz13 in forum Java IDEs
    Replies: 2
    Last Post: August 27th, 2009, 07:34 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, 12:36 PM

Tags for this Thread