Results 1 to 8 of 8
  1. #1
    Hossein is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Question When to define object and when to use "new" only

    Hi,
    Suppose we have a class, "SampleClass" containing a, say, public void method "f()". If I wanted to call this method, I could use a SampleClass sc = new SampleClass(); then call f: sc.f(); or I could just say: new SampleClass().f();

    second one seems better when I just want to use the f() and nothing else. It seems better for garbage collection (?)
    On the other hand say some time later I come back and I need to change that piece of code. (say I need another call to the f() method somewhere else in the same block of code) then this would have been better to just define the object in the first place. This means the second approach makes my code less maintainable.
    Now the question is which approach would be better?

    Thank you.
    Last edited by Hossein; 04-21-2010 at 06:53 PM. Reason: Syntax error! Forgot the parentheses for new SampleClass().f() . oops!

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    The first one. new SampleClass.f(); won't compile ;)

    Seriously, if you use new SampleClass().f(); then f() should be static.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    It depends.
    That is, it depends on your requirements. There are no hard and fast rules, however in general I would question a class like that in any case. This construct makes sense only as part of a chain. A single call (as in your example) would make me think there's something up with your model.

    So:
    Java Code:
    new SomeClass().doSomething(myParam).doSomethingElse(myOtherParam).andFinallyProcessItAll();
    might make sense (not that I think I've ever written code that works like that).

    And, in any case, garbage collection would have nothing to do with it...you're still creating an object.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Hossein View Post
    Hi,
    Suppose we have a class, "SampleClass" containing a, say, public void method "f()". If I wanted to call this method, I could use a SampleClass sc = new SampleClass(); then call f: sc.f(); or I could just say: new SampleClass.f();

    second one seems better when I just want to use the f() and nothing else. It seems better for garbage collection (?)
    On the other hand say some time later I come back and I need to change that piece of code. (say I need another call to the f() method somewhere else in the same block of code) then this would have been better to just define the object in the first place. This means the second approach makes my code less maintainable.
    Now the question is which approach would be better?
    If you want to be able to do SampleClass.f() the method f() should be a static method but the core of OO programming is all about objecs. The old SmallTalk language defined it this way: when you want to say something (a method) you need something (and object) to say it to and it should listen to what (the method) you have to say.

    A good OO based program has a bunch of somethings (objects) that talk to each other (by using) methods) to get the job done. Never think about garbage collection or other implementation details when you design your classes and how they cooperate. Garbage collection is simply there and should do its job and it should do it well.

    kind regards,

    Jos

  5. #5
    Hossein is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default Thank you all!

    new SampleClass.f(); won't compile
    Sorry, my bad. Its new SampleClass().f(); I edited the post.
    Now it works. (and shouldn't be static because of "new" which makes it a non-static call and SampleClass() which now is a constructor with parentheses)
    in any case, garbage collection would have nothing to do with it...you're still creating an object.
    Well, I thought it could make a little difference since the second one does not have a reference after the method is done executing, so GC would collect it even if it starts collecting when the corresponding block of code is being executed. (I know its not a big deal, I agree with that)
    Never think about garbage collection or other implementation details when you design your classes and how they cooperate. Garbage collection is simply there and should do its job and it should do it well.
    Yeah, Thank you I like it!!!! (lazy guy, less thing to worry about.:D)

    Thank you all.

    I still don't feel I have a straightforward answer, due to the syntax error in my post. Which one is the better way of coding? You know... considering the efficiency and conventions and stuff.

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    The first example I can think of that relates is Random. Even if I had only one method that used only one nextInt() in my class, and I expected that method to be called only once in the object's lifetime, I don't see myself doing new Random().nextInt(). I think I would probably still have a Random instance variable and initialize it in my constructor. That's mostly habit, but a lot of programming is about good habits. It would be mentally jarring to me to see it the other way, and I'd spend too much time thinking about why it's being done differently. On the other hand, if I saw a FileReader instance variable that was only being used in a BufferedReader, it would be equally jarring in the other direction. I would say develop good habits, and don't spend too much time thinking about special cases until you need to.

    -Gary-

  7. #7
    Hossein is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    Thanks Gary! ;)

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    I was actually trying to think of somewhere I had seen this.
    I originally thought of Criteria in Hibernate, but realised that you get a Criteria object from the session. Except you often do not assign this to a variable, simply using the Criteria straight away...and I'm not sure the session part actually assigns the new Criteria() (OK, whatever concrete Criteria class we're using) to a variable...so it is (sort of, if you squint) comparable. Apart from the fact that you are passing a reference around.

    However, I really can't think of a good reason to use this in (say) a single method...and cue someone to contradict me!
    :)

Similar Threads

  1. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  2. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 07:50 AM
  3. Error showing "Object Required"
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 09-26-2009, 02:23 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •