*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in InnerClass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in InnerClass" Watch "doubt in InnerClass" New topic
Author

doubt in InnerClass

Ramnath krishnamurthi
Ranch Hand

Joined: Jan 22, 2003
Posts: 56
Hi,
I thought the following code would give a compilation error since am accessing the static member of an outer class in the inner class.As per InnerClass definitions it cannot have static declarations of any kind.Is it not applicable to the static members from outer class??Please help me
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Ramnath krishnamurthi:
I thought the following code would give a compilation error since am accessing the static member of an outer class in the inner class.

There's no such restriction.
Remember what 'static' means - members belonging to the class, not instance. For a variable, its value has no bearing on the state of the instance.
So in your example, this statement is also valid:
Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
I just attempted to compile the code with the comments (//) removed ie:

I got this error message:inner classes cannot have static declarations.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Dave
Accessing a static variable is allowed in an inner class but you can not declare a static variable in a non-static inner class. So displaying the value of x, declared outside of the inner class, is fine but declaring a static variable in a non-static inner class is not allowed.
Ramnath krishnamurthi
Ranch Hand

Joined: Jan 22, 2003
Posts: 56
Hi
Thank you all.
btw,Hey Anupam did u ever work in cisco systems??I know a Anupam Sinha who worked with me @ cisco systems??
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Ramnath
Well no. I have till date not worked with Cisco Systems.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: doubt in InnerClass
 
Similar Threads
why private member variable of inner class can't be accessed by its outer class but mehod can be?
Inner Class
Private access in InnerClass
why private member variable of inner class can't be accessed by its outer class but mehod can be?
Inner class basics. (Marcus Green's mock exam 2)