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 Doubt 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" Watch "Doubt" New topic
Author

Doubt

Raj Neets
Ranch Hand

Joined: Nov 12, 2002
Posts: 46
Hello guys
I have a question regrading the inner class created inside a method.My book says that the Object of the inner class outlives the method. so i created an example.
class outer{

Object raj(){

final int a=1;

class inner {
void mmethod(){
System.out.println("Final "+a);
}
}
return new inner();
}
}
public class test {
public static void main(String z[]){
outer out = new outer();
Object ob=out.raj();
ob.mmethod();
}
}

Iam creating a class inside a method and trying to keep that class object outlive the method, so iam returning the object created inside the method and storing it in variable reference of type Object, when i tried to invoke the method with that object reference it is throwing an expection as follows
symbol : method mmethod ()
location: class java.lang.Object
ob.mmethod();

can any one help me, i want to invoke the method using the object reference.I even tried casting the object reference.
[ January 19, 2003: Message edited by: Raj Neets ]
[ January 19, 2003: Message edited by: Raj Neets ]
Rich Raposa
Ranch Hand

Joined: Dec 06, 2001
Posts: 46
No exceptions are being thrown, since that can only happen at runtime, and there's no way that code you posted is going to compile. I think you meant to say "compiler error".
If you define a class inside a method, than the scope of the definition of that class is only within that method. To try and use the "inner" class definition inside main() in a different class is not valid.
However, the Object that you returned from the inner class is definitely a valid Object. You just won't be able to use it to invoke the mmethod(), since you won't be able to cast the reference to type "inner".


Rich Raposa<br /><a href="http://www.javalicense.com" target="_blank" rel="nofollow">http://www.javalicense.com</a><br />Buy Java courseware -> get a free XBox!
Raj Neets
Ranch Hand

Joined: Nov 12, 2002
Posts: 46
hai Rich
As u said, its a compiler error, but what iam trying to understand is, Inner class object that is declared inside a method outlives the method, thats why it(inner class member variables)accesses only the final variable of the method.If we say it outlives the method life, then how can we uses the Object out side the method?
Please someone clarify my doubt.
Thanx
[ January 19, 2003: Message edited by: Raj Neets ]
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
Originally posted by Raj Neets:

Inner class object that is declared inside a method outlives the method, thats why it(inner class member variables)accesses only the final variable of the method.If we say it outlives the method life, then how can we uses the Object out side the method?

Raj
In order for an object of a local class to be usable outside of the method that created it, the local class must extend a class or implement an interface that is accessible outside the method.
If you do that, you can return your local class as the the type that it extends or implements. Or you could return it as the Object type, as you did, and have the caller cast it to the type that it extends or implements.
HTH


SCJP 1.4
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
Here is a small example:
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Hi, i am interested in this too;
a little question to clarify - when you type private Runnable, are you saying that what comes next is a variable of type Runnable?


giddee up
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
Originally posted by Jasper Vader:
Hi, i am interested in this too;
a little question to clarify - when you type private Runnable, are you saying that what comes next is a variable of type Runnable?

private Runnable createRunnable(final String value) {
createRunnable is a private method that returns an object that implements the Runnable interface
Raj Neets
Ranch Hand

Joined: Nov 12, 2002
Posts: 46
Thanx John.
beautiful reply, my doubt is clear.
 
 
subject: Doubt
 
Similar Threads
Inner class access specifier
Final Classes
outer class from inner class
Question about "Getting in Touch with your Inner Class "
Inner class