• 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

Where to create objects

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I have a question about where to create objects, I m wondering whether it is better to create an class variable in instance, or to put the direct declaration in a method if the object is used by only one method of the class.
I m wondering about this because it might be more efficient to create a class variable (and hence an object of the class which method is called) in the single method interested in in using it.
Hope the question was clear, Daniel
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan, one of the points of Java programming is to efficiently utilize your system memory. Your point about instantiating objects in only the methods that require them is valid, however you have to look at the scope of the project. You may ask yourself, does this use up more memory if I declare the Object now or declare it in the main method? I usually try to only create "new" memory space when it is absolutely necessary. Soon you will learn about big O notation and omega and theta and little o for better memory usage. But for now, just stick to the formats that you are instructed with. You will soon see which is the best way to do it.
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If, as you say, only one method needs the variable, and as long as the value does not need to persist between successive calls to that method, then yes definitely put it inside the method. That's better design, plus then you needn't worry about possible threading issues as well, as each thread invoking the method will get their own copy.
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yo, BB, sorry it got out of hand on the other page bro. I shouldn't have said anything. You are a great contributor to this forum.
You da man bro.
Gabe
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic