Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

set and get methods

 
Samuel Attah
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samuel,

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)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setEmployeeID(String employeeid)
{
this.employeeid=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 www.corej2eepatterns.com

Hope this helps.

- Senthil
 
Senthil Rajendran
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
small correction

public String setEmployeeID() // signature should be getEmployeeID
{
return employeeid;
}
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this discussion to "Java in General (intermediate)" forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic