Results 1 to 19 of 19
  1. #1
    Bigfatcat is offline Member
    Join Date
    Apr 2011
    Location
    UK
    Posts
    10
    Rep Power
    0

    Default new MyClass() -vs- MyClass mc = new MyClass()

    Hi all,

    Whenever possible or appropriate I create a new instance of an object without defining it (see 'a' below):
    a)
    Java Code:
    new MyClass();
    I'm not sure why but I've always assumed that when possible it is more efficient to instantiate an object that way ('a') rather than the usual defining of the variable (see 'b' below):
    b)
    Java Code:
    MyClass mc = new MyClass();
    Is my assumption right? Or when I've seen others do this has it simply been for aesthetic reasons?

    Also is there an official name for this use? In my testing I have one method using 'a)' and another using 'b)' in order to experiment with the differences in random number generation but I've struggled to pick method names and javadoc descriptions which make obvious the difference between the two methods. I realise that it probably doesn't matter that much but I'm a little anal about it.

    I'm not new to java so I should probably know this; I have an excuse somewhere, but I think my dog ate it.

    Thanks in advance,
    Regards,
    BFC
    Last edited by Bigfatcat; 04-20-2011 at 01:48 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Both of your approaches are creating a new instance of MyClass. The only difference is that your "b" approach keeps a reference to that Object.

    There are a few cases where your "a" approach is useful (I guess), but for the most part you're going to want to do something with a reference, so you're going to use the "b" approach.

    This has nothing to do with efficiency. If you need a reference, then you need a reference.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Method b allows you to create multiple of the same object type to references.
    ie
    Java Code:
    Dog dog1 = new Dog();
    Dog dog2 = new Dog();
    I personally never use method a, but I don't see many practical applications.

    EDIT: Too slow.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    You'd use method "a" is when you don't need a reference to the instance and can just throw it away when you're done, like when you pass it into a method. Something like:

    Java Code:
    panel.add(new JLabel("Some Text"));
    This uses the pretty much the exact amount of memory as if you did this:

    Java Code:
    JLabel l = new JLabel("Some Text");
    panel.add(l);
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Bigfatcat is offline Member
    Join Date
    Apr 2011
    Location
    UK
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    You'd use method "a" is when you don't need a reference to the instance and can just throw it away when you're done, like when you pass it into a method. Something like:

    Java Code:
    panel.add(new JLabel("Some Text"));

    This uses the pretty much the exact amount of memory as if you did this:


    Java Code:
    JLabel l = new JLabel("Some Text");
    panel.add(l);

    That's the ticket, cheers :) and cheers all.

    I thought "surely it takes more resources - albeit a nominal amount - to store a reference". Nevertheless I understood it wouldn't affect me, I was just wondering whether it was good coding practice for a reason other than neatness. But...it seems from the replies in this thread that it is a practice not overly used by others, I'm surprised as I quite like it.
    Last edited by Bigfatcat; 04-20-2011 at 02:08 PM.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Storing a reference might take a tiny tiny tiny bit more memory, but it's going to be negligible compare to the instance it's referencing.

    However, this doesn't mean you should have global references to every instance of every Object you use. You want the garbage collector to get rid of old instances you aren't going to use anymore, so make sure you use references in as narrow a scope as possible. If you only use an Object inside a method and then never again, only declare that reference inside the method.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    A reference takes bits of memory. Varying only very little by the type of reference and contents. Not enough to notice, especially with the average computer today.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,001
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Bigfatcat View Post
    That's the ticket, cheers :) and cheers all.

    I thought "surely it takes more resources - albeit a nominal amount - to store a reference". Nevertheless I understood it wouldn't affect me, I was just wondering whether it was good coding practice for a reason other than neatness. But...it seems from the replies in this thread that it is a practice not overly used by others, I'm surprised as I quite like it.
    If you do 'new Foo()' that new Foo object, if it want to survive its construction time has to claw itself away from the grim gc reaper, i.e. it has to create references (one is enough) to itself during construction time.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Bigfatcat is offline Member
    Join Date
    Apr 2011
    Location
    UK
    Posts
    10
    Rep Power
    0

    Default

    Thanks all...so to answer my anal question, is there a 'name' for instantiating without defining a reference? It's quite a mouthful as-is.

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Quote Originally Posted by Bigfatcat View Post
    Thanks all...so to answer my anal question, is there a 'name' for instantiating without defining a reference? It's quite a mouthful as-is.
    :eek: What a delightful (to my inner immature teenager) typo and word choice combo.

    But to answer your question, I don't think so. You're just instantiating an Object without storing a reference to it. No special name. Make one up!
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Bigfatcat is offline Member
    Join Date
    Apr 2011
    Location
    UK
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    :eek: What a delightful (to my inner immature teenager) typo and word choice combo.

    But to answer your question, I don't think so. You're just instantiating an Object without storing a reference to it. No special name. Make one up!
    Lol anal, as in, obsessive/compulsive, not rear-end. That's right...isn't it? :o ...granted, my choice of words afterwards could have been better. I'd search google to make sure I'm using the correct spelling of 'anal' but I'm scared of what will turn up.

    Cheers by the way, make one up I will.

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,001
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Bigfatcat View Post
    Lol anal, as in, obsessive/compulsive, not rear-end. That's right...isn't it? :o ...granted, my choice of words afterwards could have been better. I'd search google to make sure I'm using the correct spelling of 'anal' but I'm scared of what will turn up.

    Cheers by the way, make one up I will.
    There is a word for the opposite: resurrection, when an object creates a reference to itself when it is about to be reaped but its finalize() method creates that reference again. (that's one of the reasons a finalize() method is only called once). Maybe 'miscarriage' will do here?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Bigfatcat is offline Member
    Join Date
    Apr 2011
    Location
    UK
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    There is a word for the opposite: resurrection, when an object creates a reference to itself when it is about to be reaped but its finalize() method creates that reference again. (that's one of the reasons a finalize() method is only called once). Maybe 'miscarriage' will do here?

    kind regards,

    Jos

    Aha I didn't know that thanks; so it is resurrected because it's 'essence' (reference) was dying. That brings up a whole load of possibilities. "Miscarriage variable" makes perfect sense although it is slightly morbid. Perhaps..."undead variable", or "zombie variable"; the variable is 'alive' but it's 'essence' (reference) is 'dead'. It's quite fun making up names for things that probably don't need them, I wonder if I could get a job doing that.

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Quote Originally Posted by Bigfatcat View Post
    Aha I didn't know that thanks; so it is resurrected because it's 'essence' (reference) was dying. That brings up a whole load of possibilities. "Miscarriage variable" makes perfect sense although it is slightly morbid. Perhaps..."undead variable", or "zombie variable"; the variable is 'alive' but it's 'essence' (reference) is 'dead'.
    Not to nitpick, but there is no variable here. The instance is "alive", but there is no reference to it (unless it passes itself as a reference to something else during construction time). "Unreferenced instance" is probably what I would call it, although that's a bit boring.

    Quote Originally Posted by Bigfatcat View Post
    It's quite fun making up names for things that probably don't need them, I wonder if I could get a job doing that.
    You should get into management or government work. They love making up words. My job has more acronyms than I can remember.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,001
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by KevinWorkman View Post
    Not to nitpick, but there is no variable here. The instance is "alive", but there is no reference to it (unless it passes itself as a reference to something else during construction time). "Unreferenced instance" is probably what I would call it, although that's a bit boring.



    You should get into management or government work. They love making up words. My job has more acronyms than I can remember.
    I bet quite a lot of them have an 'X' in them ... b.t.w. a baby dangling on a fishing rod on a very thin line seems (more or less) appropriate for the new WhatEver() thing to me (but that's just me ;-)

    kind regards,

    Jos (<--- totally sick person)
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Actually, I was thinking about it, and the instance is one that will be destroyed pretty much right after it's created... so a doomed baby is not a bad metaphor.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,001
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by KevinWorkman View Post
    Actually, I was thinking about it, and the instance is one that will be destroyed pretty much right after it's created... so a doomed baby is not a bad metaphor.
    I asked my wife about it; she thinks I have lost my mind long ago and she came up with the analogy of a cartoon figure, sitting on a branch while cutting it off on the wrong side. I think I like that too ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    Bigfatcat is offline Member
    Join Date
    Apr 2011
    Location
    UK
    Posts
    10
    Rep Power
    0

    Default

    Thank goodness, I thought I was the only one sent nutty by java.

    "Doomed baby" made me laugh, I'm not sure it will catch on though, we could write up a shortlist and send it to the 'Java-gods' for them to choose their favourite.
    In a few years time some new forum member - like me, will ask "what's the point in doomed babies?" and you Kevin can explain, you'll be famous for it.

    NB: No worries about being picky, programmers have to be. So it's not a variable it's a miscarriage/undead/zombie/ghost/doomed-baby/baby-dangling-on-a-fishing-rod-on-a-very-thin-line instance.

  19. #19
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,001
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Bigfatcat View Post
    NB: No worries about being picky, programmers have to be. So it's not a variable it's a miscarriage/undead/zombie/ghost/doomed-baby/baby-dangling-on-a-fishing-rod-on-a-very-thin-line instance.
    You forgot: with its throat slit just before it landed in a meat grinder never to be seen again.

    kind regards,

    Jos (<--- my wife disagrees here ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 01-21-2011, 06:22 AM
  2. define list<myclass> in xhtml
    By miq.system in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-26-2009, 03:02 PM
  3. return myClass
    By aldo in forum New To Java
    Replies: 3
    Last Post: 12-21-2008, 06:22 PM
  4. Implements MyClass extends JFrame
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 03:43 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
  •