Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt

 
Raj Neets
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Raj Neets
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
John Paverd
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a small example:
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
John Paverd
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx John.
beautiful reply, my doubt is clear.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic