aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Local inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Local inner classes" Watch "Local inner classes" New topic
Author

Local inner classes

sajida kal
Ranch Hand

Joined: Mar 22, 2001
Posts: 89
Hi,
Velumurugan's study notes on Local classes states .. "Since the names of local classes are not visible outside the local context, references of these classes cannot be declared outside. So their functionality could be accessed only via super-class references (either interfaces or classes). Objects of those class types are created inside methods and returned as super-class type references to the outside world. This is the reason that they can only access final variables within the local block. That way, the value of the variable can be always made available to the objects returned from the local context to outside world."
To understand the above, I wrote the following piece of code,
declaring a local class in a method. The method returns an
instance of the interface Inter implemented by local class
a1.

Please note the commented out lines in the code. If I uncomment the first, I get the compiler error "No variable s defined in
interface Inter". Similarly, if the second line is uncommented, the error thrown is "Method method1() not found in interface Inter". Since method2() returns an instance of Inter, the errors
are understandable. In what way can main access class variable s or method method1() ? ie how can we access the functionality of the local class via the super class reference?
Hope someone can clarify my doubts.
Thanks !
Sajida
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
interface Inter
{
static String b = "Final String";
public void lap();
}
public class Class4
{
public static void main(String args[])
{
Class4 cl = new Class4();
Class2 c2 = new Class2();
Inter i=c2.method2();
System.out.println(i.b);
i.lap();
Inter b =c2.method2();
// b.method1();
}
}
class Class2
{
Inter a;
Inter method2()
{
class a1 implements Inter
{
final String s = "Final string in class a1";
a1()
{
System.out.println("String s is "+s);
}
public void lap()
{
System.out.println("what r u doing");
}
void method1()
{
System.out.println("Method 1 from local class");
}
}
a=new a1();
return a;
}
}

Hi sajida,
I have modified your code a little in order to make it runnable.I think the mistake which u made in the above given code is that u were trying to access the varaible s of class2.I am sure about the explaination which i am providing but here it comes,u can only access those method's and varaible's in your class class2 which has been declared in the interface Inter.U cannot access the varaible s through the varaible of interface type(Inter) because it's simple not there.Remeber one thing varaible calling is determined by reference type and method calling is determined by object of the class.
i have declared a method lap in interface Inter whose implementation has benn provided in the class Class2.
just to make it more simpleer, see the programmes given below.
interface i
{
}
class nit implements i
{
int s=12;
public void lap()
{
System.out.println("method in class lapore");
}
public static void main(String[]args)
{
i p=new nit();
p.lap();
}
}
The above given code gives a compile time error because interface i have no method named lap, so u cannot call a method lap through using a reference varaible of type i.
Now have a look at the code below:-
In the below given code,i have declared a method named lap in interface i whose implementation is given in the nit class implementing interface i,therefore, no compile time error.
interface i
{
public void lap();
}
class nit implements i
{
int s=12;
public void lap()
{
System.out.println("method in class lapore");
}
public static void main(String[]args)
{
i p=new nit();
p.lap();
}
}
sajida kal
Ranch Hand

Joined: Mar 22, 2001
Posts: 89
Hi Nitin,
Thanks for your answer. So essentially, accessing the functionality of the local class implies that we can access the methods implemented by the local class. And in keeping with the
philosophy of hiding the implementation, the variables of the local class cannot be accessed since we are ensuring that a
reference to the superclass/interface is being returned.
Okay, so thats clear now!
Thanks a ton!
Sajida
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Local inner classes
 
Similar Threads
Can any one please explain this
LOCAL inner class
an urgent inner class question
static variable: can I always get its value ??
Protected members