• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple question on class variables accessability.

 
Xyz Abx
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In our application we have lot of value objects ,which holds all private variables and corresponding getter and setter methods).These objects does not contain any business logic.My question is why we need getter and setter method instead of making all variables visibility to public, so that i can access variable directly using object.variable fashion.
Thanks
chittaranjan.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi chittaranjan,
welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chittaranjan:
In our application we have lot of value objects ,which holds all private variables and corresponding getter and setter methods).These objects does not contain any business logic.

This is typically an indicator for a less than optimal object oriented design. Why don't those objects contain business logic?

My question is why we need getter and setter method instead of making all variables visibility to public, so that i can access variable directly using object.variable fashion.

As long as you don't need to change something, there is really no difference.
Once you need to change the implementation of the objects, accessor methods will give you more flexibility, though. You could calculate a value on demand, cache it, load it lazyly etc. You could even use different implementations at the same time.
But, again, the pure existence of many accessor methods is a design smell. Instead of asking objects for data, you should typically be able to tell them to do something with their own data.
 
Xyz Abx
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ilja.
 
Xyz Abx
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja:
-------------------------------------------------
This is typically an indicator for a less than optimal object oriented design. Why don't those objects contain business logic?
-------------------------------------------------
These value objects holds data returned by entity beans and transfered to client (applet) to reduce network traffic.
[ May 03, 2003: Message edited by: Chittaranjan ]
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic