| Author |
instantiating class without declaring variable
|
nancy cochran
Greenhorn
Joined: Feb 10, 2006
Posts: 10
|
|
I'm used to instantiating objects this way, MyClass myClass1 = new MyClass(); A co-worker tells me its ok to call a method of the class without instantiating as above, but by using MethodReturnVariable myVariable = new MyClass().mymethod(); In this case, mymethod is a method of MyClass which returns MethodReturnVariable. Is this valid? Can anyone explain what happens when this is done? thanks
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
|
If you want to call an instance method of a class, then you must use an instance of the class to do it. In your first code, you are creating an instance of the class, and keeping the reference to it. In the second code, you are creating an instance of the class, but not maintaining the reference.
|
 |
Kj Reddy
Ranch Hand
Joined: Sep 20, 2003
Posts: 1697
|
|
|
Yes that is valid. Some people do that when instance of MyClass is just used for mymethod() call and no other purpose. It create instance of MyClass object with no reference(so we can not use this object again) and calls mymethod on it.
|
 |
nancy cochran
Greenhorn
Joined: Feb 10, 2006
Posts: 10
|
|
|
If there is a finalize method in the class, when would it be called using both methods?
|
 |
Mani Ram
Ranch Hand
Joined: Mar 11, 2002
Posts: 1140
|
|
The code is same as In either case, the JVM creates an object of MyClass. In first example, you don't have a reference to that created object, where as in the later case, you get a reference to the MyClass object, which you can use it at a later stage, if necessary. If I see lot of Style 1 in the code, I will consider it as a code smell, because you can use that style, only when you have throw away objects (meaning, the state of that object is not necessary), which is bad design, IMHO. If they are mere utility methods, then I would generally prefer to make those methods static and call them simply as [ March 17, 2006: Message edited by: Mani Ram ]
|
Mani
Quaerendo Invenietis
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
If they are mere utility methods, then I would generally prefer to make those methods static ...
This answer is not wrong, but static methods have some limitations in flexibility. It might be worth keeping a real object and abstracting its creation: Factory.getInstance().someMethod(); Now you've removed your hard coded dependency on the implementation class. Factory could return any subclass of MyClass, or better yet any implementation of MyInterface. In the beginner forum I wouldn't recommend building this kind of flexibility all the time, but keep it in the back of your head somewhere. If you bump into something like that, figure out what kind of flexibility the author was trying to keep open. I know I've made a couple classes with all static methods that I regretted later because they were too hard to change.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Mani Ram
Ranch Hand
Joined: Mar 11, 2002
Posts: 1140
|
|
Originally posted by Stan James: This answer is not wrong, but static methods have some limitations in flexibility. It might be worth keeping a real object and abstracting its creation:
Totally agree. But this being a beginner forum & I was lazy to type a big explanatory post, I just skipped it
|
 |
nancy cochran
Greenhorn
Joined: Feb 10, 2006
Posts: 10
|
|
|
thanks for all of your input! this is a really great forum!
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Originally posted by Mani Ram: The code is same as In either case, the JVM creates an object of MyClass. In first example, you don't have a reference to that created object, where as in the later case, you get a reference to the MyClass object, which you can use it at a later stage, if necessary.
So it's not totally the same - in style 1, the object is eligible for garbage collection immediately after mymethod returned. In style 2, it is garbage collectible only after myClass1 gets out of scope.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
 |
|
|
subject: instantiating class without declaring variable
|
|
|