my dog learned polymorphism
The moose likes Java in General and the fly likes set and get methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "set and get methods" Watch "set and get methods" New topic

set and get methods

Samuel Attah

Joined: Nov 23, 2004
Posts: 1
Hi there,
Any body have an idea of how to implement set and get methods. I am douting about how the set and get methods be written in a class. Should both methods be used in a constructor or in the main.
Note: I want my main to be in another file.
Hope to hear from any body who have experience this problem before.
Bye for now Samuel.
Senthil Rajendran
Ranch Hand

Joined: Nov 03, 2004
Posts: 35

This pattern is called as Value Object, Example is as follows

public class EmployeeVO
private String name;
private String employeeid;

public void setName(String name)
public String getName()
return name;
public void setEmployeeID(String employeeid)
public String setEmployeeID()
return employeeid;
} // No main method is present here

Ideally you extract the data from a request object in the web tier, create a instance of the above EmployeeVO and assign the data using the setter methods and send the EmployeeVO object as the parameter to the Data Acess Tier.In Data Acess Tier its retrived using gettter methods.

Sameway when you retrive data from a database the same process is done and the EmployeeVO object is sent to the Web Tier

Dont write getter and setter methods in an EJB, its a costly deal !

This pattern is also out dated ... you can use Transfer Object, Its quite the same except that it has only member variables (No getter & setter methods) would say its not encapsulated. For more details on this refer

Hope this helps.

- Senthil
Senthil Rajendran
Ranch Hand

Joined: Nov 03, 2004
Posts: 35
small correction

public String setEmployeeID() // signature should be getEmployeeID
return employeeid;
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
Moving this discussion to "Java in General (intermediate)" forum.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
I agree. Here's the link:
subject: set and get methods
jQuery in Action, 3rd edition