This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes local classes.............. 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 "local classes.............." Watch "local classes.............." New topic

local classes..............

Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
A class that resides within a method can only access final variables.....right?
The reason is, I'm looking at some code now that is implying that you can access final variables and instance variable of the outer class!?
Can an local class within a method of another class access that classes instance variables?
ps - the fear is upon me, exam on wednesday, and I did nothing on the weekend, due to hangover.......

The early bird may get the worm, but the second mouse gets the cheese.........<br /> <br />Sun Certified Programmer for Java 2 Platform<br />Sun Certified Web-Component Developer for J2EE Platform
amol re
Ranch Hand

Joined: Mar 21, 2002
Posts: 36
1. Yes. The local class does have access to the outer class variables.
2. If the local class is a non static class, it gets access to all the outer class variables.( static as well as non static ) But for this local class to be a non static class, the method encompassing it has to be a non static method so that outer context can be made available to the local class.
3. If the encompassing method is a static method, the local class *has* to be static. In that case, it can access only static variables of the outer class.
4. all this is in addition to the access it always gets to the final local variables declared in the encompassing method.
Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
public class Outer
private int i = 9;
public Outer()
public doSomething(final int f)
class Inner
System.out.println(f) //legal - final
System.out.println(i); //I was under the impression that this would be illegal, are you saying that a class defined in a method has access to the outer classes variable?

Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi Rowan,
A local class defined in non-static method can access all members in of the outer class and local final variables.

A local class defined in a static method can access static members and local final variables.

Hope this helps,
Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
that's great! Cheers!
jQuery in Action, 2nd edition
subject: local classes..............
Similar Threads
doubts from the john hunt's mock test
final local variable question
nested inner classes
Method local inner class and accessing local variables
Wrong Answer?