aspose file tools*
The moose likes Struts and the fly likes When I must use Getters and Setters? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "When I must use Getters and Setters?" Watch "When I must use Getters and Setters?" New topic
Author

When I must use Getters and Setters?

André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
Hi Guys,

When I must use Getters and Setters? When I need to access any attribute and increment?

ex:

public class Account{
String name;
int id;

}

---------------------------
Class Manager{
Account a = new Account ();
a.name('André AS');
}


Do I need to you set and get for acess id attribute?

André AS
???


André AS
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I don't see how this is Struts-specific.
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
Set and Get...
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

David Newton wrote:I don't see how this is Struts-specific.

I don't think it is.

André Asantos wrote:Do I need to you set and get for acess id attribute?

You won't, if the Account and the Manager classes are both in the same package, because "String name" and "int id" have default access modifier. So you can access "id" directly.

If your Account class contains only name and id, then you can make these as "private" and then have get and set methods for them.
 
jQuery in Action, 2nd edition
 
subject: When I must use Getters and Setters?
 
Similar Threads
jdbc and nearly global variables question
public variables
Problem formatting dates.
Problem in usebean
Documentation & Coding Styles