• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why doesn't named classes classes doesn't insist on final parameter variable?  RSS feed

 
Rajkumar Masanaiyan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Annonymous inner classes require local variables passed to the class to be final since the scope is different and the annonymous classes can exist even after the local variables go out of scope. Doesn't the same consideratiosn apply for named outer classes? Would you please explain how the out classes manage to work with out of scope local variables.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They can't - once a variable is out of scope it's gone. With a method-local (anonymous) class you can refer to method-local variables, which is where the problem arises. In normal classes you can't refer to them at all - final or not.

Can you give an example where you think they do, because maybe I misunderstand what you mean?
 
Rajkumar Masanaiyan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am now having two more question:

1) if I create a new thread object, can I pass local variable to it's constructor?

2) aren't inner classes instantiated in the same method, whose local variable it refers. When the method returns, wouldn't the inner class object also go out of scope (with the exception of multithreaded objects)?

Regards,
Ravi
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravisankar Sivasubramaniam wrote:I am now having two more question:

1) if I create a new thread object, can I pass local variable to it's constructor?

2) aren't inner classes instantiated in the same method, whose local variable it refers. When the method returns, wouldn't the inner class object also go out of scope (with the exception of multithreaded objects)?

1) What happened when you tried it?

2) remember there is a difference between the object and the reference. It is possible to create an object (with or without an innner class) inside a method and save the reference somewhere else - in a collection, for example. Then, even when you leave that method, the collection still holds an active reference (assuming the collection is active), and thus the object does not go out of scope.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravisankar Sivasubramaniam wrote:1) if I create a new thread object, can I pass local variable to it's constructor?

Sure, why not? What happened when you tried it?
2) aren't inner classes instantiated in the same method, whose local variable it refers. When the method returns, wouldn't the inner class object also go out of scope (with the exception of multithreaded objects)?

Meaningless. Objects never go "out of scope". Variables can and do go out of scope, but not the objects they might have referred to.
 
Rajkumar Masanaiyan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
1) What happened when you tried it?


Compiler was very happy accepting a non-final local variable as a constructor parameter for a thread object; didn't complain a bit. The code is pasted at the bottom for your reference.

fred rosenberger wrote:
2) remember there is a difference between the object and the reference. It is possible to create an object (with or without an innner class) inside a method and save the reference somewhere else - in a collection, for example. Then, even when you leave that method, the collection still holds an active reference (assuming the collection is active), and thus the object does not go out of scope.

In the example below, the reference to the anonymous class is held in the local variable list1. Once the main function returns, list1 will go out of scope and so there is no guarantee that the anonymous class object will exist for any specific period of time. This is where I need clarification.

---------

 
Rajkumar Masanaiyan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Ravisankar Sivasubramaniam wrote:1) if I create a new thread object, can I pass local variable to it's constructor?

Sure, why not? What happened when you tried it?


Paul: When we pass a local reference (eg: Employee emp = new Employee()) to the constructor of a long running thread object and the calling function completes soon after creating the thread object, wouldn't the reference in the thread object point to a non-existing object (eg: the object that was previously pointed to by emp). I thought anonymous inner class insists on final parameters to avoid exactly this situation? Where in my interpretation am I going wrong?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Sivamani wrote:When we pass a local reference (eg: Employee emp = new Employee()) to the constructor of a long running thread object and the calling function completes soon after creating the thread object, wouldn't the reference in the thread object point to a non-existing object (eg: the object that was previously pointed to by emp). I thought anonymous inner class insists on final parameters to avoid exactly this situation? Where in my interpretation am I going wrong?


The constructor isn't run in a different thread, so passing a variable in to that is just the same as passing it to any method. It still won't be accessible to the run() method of the thread, so can't be used "out of scope". If you want to make it available to the run() method, you'd then assign it to a member variable of the anonymous class - but now you've got a variable that's guaranteed to be in scope, and so isn't a problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!