The Artima Developer Community
Sponsored Link

Java Answers Forum
Inner classes - private field access

2 replies on 1 page. Most recent reply: Oct 10, 2006 7:36 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Johnny Bravo

Posts: 1
Nickname: psynaps3
Registered: Oct, 2006

Inner classes - private field access Posted: Oct 7, 2006 3:53 AM
Reply to this message Reply
Advertisement
Hi,

I have a small query regarding inner classes and access to private fields.

Eg:

class C {
private int varC;
}

public class A {
private int varA;

private class B {
private int varB;

public void accessA {
// Access private variable of A
Sysout(varA);
}

}

public void accessB {
// Access private variable of B - why is this possible?
Sysout(new B().varB);

// As expected, the below doesn't work
Sysout(new C().varC);
}
}


I would like to know why/how class A is able to access the private field 'varB' of its inner class B. I assume that the field 'varB's scope would be confined to that of class B. Shouldn't the behaviour have been consistent with trying to access the private field 'varC' of class C from within a member function of class A?

Thank you for your time.


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Inner classes - private field access Posted: Oct 9, 2006 11:17 PM
Reply to this message Reply
> Hi,
>
> I have a small query regarding inner classes and access to
> private fields.
>
> Eg:
>
> class C {
> private int varC;
> }
>
> public class A {
> private int varA;
>
> private class B {
> private int varB;
>
> public void accessA {
> // Access private variable of A
> Sysout(varA);
> }
>
> }
>
> public void accessB {
> // Access private variable of B - why is this
> this possible?
> Sysout(new B().varB);
>
> // As expected, the below doesn't work
> Sysout(new C().varC);
> }
> }
>
>
> I would like to know why/how class A is able to access the
> private field 'varB' of its inner class B. I assume that
> the field 'varB's scope would be confined to that of class
> B. Shouldn't the behaviour have been consistent with
> trying to access the private field 'varC' of class C from
> within a member function of class A?
>
> Thank you for your time.


B is an Inner class of A and is hence in the global scope
of A.

C is not the same case.

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Inner classes - private field access Posted: Oct 10, 2006 7:36 PM
Reply to this message Reply
Any declaration that is contained within another declaration (container declaration) can be accessed by the container declaration.Since B and varB are declared within A, A can access B and varB even though varB is private.

Please see the following explanation where SuN's team declaraed it as no bug and interpreted it as valid based on specification.

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4510902

Flat View: This topic has 2 replies on 1 page
Topic: Emailing an Order in Java Previous Topic   Next Topic Topic: calling javac from VB.NET

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use