• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Ranch Hand
Posts: 923
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
should domain object(of POJO) be created inside method or as instance variable of class?
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • 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
  • 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: 923
2
  • Mark post as helpful
  • send pies
  • 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
  • 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
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • 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: 923
2
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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'".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!