This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Inner class code example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inner class code example" Watch "Inner class code example" New topic
Author

Inner class code example

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi

I tried out the following code :



The result is 21. My question is how can class Gazer access a static variable? I mean we are trying to access a static member of the outer class from a non static context. So it should throw an exception right?


~ Mansukh
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

There's never anything wrong with accessing a static member from a non-static context.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Paul Clapham wrote:There's never anything wrong with accessing a static member from a non-static context.


Ohhh.. Ok.. So the problem is when we try to access a non static member from a static context. Correct Paul? That will upset the compiler right?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Mansukhdeep Thind wrote:So the problem is when we try to access a non static member from a static context. Correct Paul? That will upset the compiler right?


Yes, I'm sure you must have seen that message already, haven't you?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Paul Clapham wrote:
Mansukhdeep Thind wrote:So the problem is when we try to access a non static member from a static context. Correct Paul? That will upset the compiler right?


Yes, I'm sure you must have seen that message already, haven't you?


Thank you Paul.
 
 
subject: Inner class code example
 
Similar Threads
SCJP Self test question: navel gazing
Method Local Inner class cannot access Final local Variables Problem
printing out array values
Question on inner classes
plz tell me about my prolem?