• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in InnerClass

 
Ramnath krishnamurthi
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ramnath
Well no. I have till date not worked with Cisco Systems.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic