This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Is this way of setting the value for setter method correct?

 
Ranch Hand
Posts: 1131
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a class Abc.java



In class Xyz.java, the property value is being set if a conditin is true


Do we have to create object of Abc abc=new Abc() as class variable and then set the value as above or this object should be created inside the method and then value is set?

thanks
 
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica. Shiralkar wrote:

Do we have to create object of Abc abc=new Abc() as class variable and then set the value as above or this object should be created inside the method and then value is set?



I'd say the way you're doing it now is right, but it really depends on your requirements.

If the value is meant to be part of the state of the object at creation, you can set it in your constructor. But if it is likely to change (or is meant to be changed), the setter / getter you're using now seems right.
 
Marshal
Posts: 65062
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is correct, as long as the class invariant does not impose any restrictions on the possible values of value. You can enhance the set method with checks for nullity, emptiness, etv.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!