Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When I must use Getters and Setters?

 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
???

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see how this is Struts-specific.
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Set and Get...
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic