• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

instantiating class without declaring variable

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there is a finalize method in the class, when would it be called using both methods?
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for all of your input! this is a really great forum!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic