• 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

Use exsisting instance or create new?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a class needs to reference the methods of a class in the same package should it create its own instance of the class, or should I try use an existing instance?

What's best practice?

e.g.

MainClass
ExampleClass1 ec = new ExampleClass1();
ExampleClass2 ec2 = new ExampleClass2();

ExampleClass2
//should I try use the same instance of ExampleClass1 e.g.
MainClass.ec.DoWhatever;

//or should I create a new instance e.g.
ExampleClass1 thisEc = new ExampleClass1();
thisEc.DoWhatever
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this question comes up, then it probably means you're doing something wrong. In good object-oriented design, it generally doesn't make sense to call a method outside of the context of a particular object. In other words, you shouldn't be wanting to just call a function; you should have an object and want to invoke a method on it for some reason.

If you understand what I'm saying, the next question is, What to do about it? Consider these three possibilities (and probably more

1) The method you want to call doesn't belong in that other class; it actually belongs in this class, where the method is convenient;

2) The code you're writing doesn't belong in this class, but rather in that other class, where it can call the method on itself.

3) In some cases, the method really doesn't need to be attached to an object; in that case it should be static. But too many static methods indicates, again, a non-object-oriented design; it's a problem that many new to Java have.
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see any advantage in creating new instances. Infact creating new instances will create new objects on the heap. My guess would be the best practice is to use the existing instance.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


When a class needs to reference the methods of a class in the same package should it create its own instance of the class, or should I try use an existing instance?

What's best practice?

e.g.




The best practice is going to depend on what it is that you are trying to accomplish.

Now, to answer your original question more specifically, whether you should use the same instance of ExampleClass1 in both MainClass and ExampleClass2 depends on whether you want them both to be using the same methods of the same object. For example, let's say you're an instance of MainClass and I'm an instance of ExampleClass2 and ExampleClass1 represents a plate of nachos. If we're both really hungry, then we'd probably each want our own plate (in other words, we'd each have a separate instance). On the other hand, if we weren't very hungry, maybe we would want to share a single plate (i.e. we'd both reference the same instance). So the way to do it depends on the circumstances--there's not One Right Way.

Also, I'd like to point out that your current method of referencing ec by using MainClass.ec.DoWhatever implies that MainClass is a static class (which is fine if that's what you want it to be, though in that case I'm pretty sure it can't contain references to instance (i.e. non-static) variables, like ec). If MainClass isn't static, then you'll need to instantiate it and pass a reference of it (I'll call it mc) to ec2 and then use mc.ec.doWhatever()--though it would probably make more sense to just pass the reference to ec directly to ec2 and use that instead of going through the mc instance. But again, that's going to depend on what, specifically, you're trying to do
[ October 13, 2006: Message edited by: Joel Jorgensen ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic