my dog learned polymorphism*
The moose likes OO, Patterns, UML and Refactoring and the fly likes simple question on class variables accessability. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "simple question on class variables accessability." Watch "simple question on class variables accessability." New topic
Author

simple question on class variables accessability.

Xyz Abx
Greenhorn

Joined: Jan 30, 2002
Posts: 12
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

Joined: Jul 11, 2001
Posts: 14112
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!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Jan 30, 2002
Posts: 12
Thanks Ilja.
Xyz Abx
Greenhorn

Joined: Jan 30, 2002
Posts: 12
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: simple question on class variables accessability.
 
Similar Threads
Java Class with all attribute as public
Call by reference related >>>>
why we do not need getter method for variable ?
Dreamcar assignment for Part II
State of variables in inheritance