This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Originally posted by Prasad Maddipatla: I didnot understand as we consider enum as a class then a private instance variable can be acessable from outside enum?
Private fields and methods are accessible anywhere within the top-level class enclosing them. This means that a private member in an inner class is accessible to the outer class, which is what happens in your second program.
More interestingly, this also means that different inner/nested classes of the same top-level class can access each other's private members. For example, you may be surprised to learn that this program is perfectly acceptable:
Hello.. I have jus started with preparing fro SCJP 1.5... According to whatever I have understood from Chap 8 of K&B Bar is a static nested class and it can be instantiated within static method as
Test.Bar b=new Test.Bar();
Please help me to solve this doubt
Kelvin Chenhao Lim
Joined: Oct 20, 2007
Hi "kssn nssk",
If you needed to use Bar outside of Test, then yes you'd need to include the outer class name as a prefix. However, since the above code uses Bar within Test itself, the prefix can be omitted. (But it's still fine to put it in.)