• 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

should domain object(of POJO) be created inside method or as instance variable of class?

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
should domain object(of POJO) be created inside method or as instance variable of class?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don’t know. You would have to provide a lot more details. But as a general rule, the smaller the scope the easier something is to handle.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

should domain object(of POJO)


That doesn't make sense. Could you elaborate with a SSCCE
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As with any variable whether it should be an instance variable or a local is determined, at least as a first cut, by whether it represents a part of the state of that object that should persist as long as the object does without regard to whether any operation is currently ongoing; or is something that is only required to perform a particular operation.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a ) suppose there is a string object which is to be used in a java method. should it be created as class instance variable or method variables?
b) suppose there is a User variable (User is POJO class with userName,UserAge).should it be created as class instance variable or method variables?
thanks
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica. Shiralkar wrote:a ) suppose there is a string object which is to be used in a java method.



If it's not going to be used in a method, there's no point in it existing in the first place, so the above is the same as, "Suppose there is a Java String object."

should it be created as class instance variable or method variables?



You're asking, "For any given Java String object that ever exists, should it be a local or a member variable?" See my previous response for the answer to that question.

b) suppose there is a User variable (User is POJO class with userName,UserAge).should it be created as class instance variable or method variables?
thanks



See my previous response.
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you have a class User with instance variables name and age. How did you decide that these should be instance variables and not local method variables? The answer to this question should help you solve your problem.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think i got the answer is : If variable has to be accessed from wider scope put it instanace variable .if it is needed only for small operation keep it method variable. Is that right?

thanks
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica. Shiralkar wrote:if it is needed only for small operation keep it method variable. Is that right?


Actually, you can go further and say "if it is only needed by one method, make it a method variable".

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Monica. Shiralkar wrote:if it is needed only for small operation keep it method variable. Is that right?


Actually, you can go further and say "if it is only needed by one method, make it a method variable".



@OP: This is a useful guideline, but you need to be aware that the converse is not necessarily true. That is, just because it's used by multiple methods, that doesn't mean it should be a member. If several different methods can perform different operations on the same data, but each one uses it only in a self-contained fashion, it should be a local. Likewise, if method X() calls Y() which calls Z() and all 3 use that variable, that doesn't necessarily mean it should be a member.

The main attribute for a variable to be a member variable is that it is part of the object's state. That is, that variable keeps its value even when no methods are running, so that at any time we can look at that object and say, "This X's Y is Z," such as "This Person's name is 'George'".
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic