The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

getters and setters

Posted by Jody Brown on November 29, 2001 at 4:19 PM

Getters and setters are the standard way of accessing a classes variables. In Class1 add the following methods:


public String getName1() {
return name;
}
public void setName1(String value) {
name1 = value;
}

Technically, the setter is only needed if you want to set the value of name1 outside of the class containing the variable. In method1, you can set name1 directly, or call the set method above.

Now, Class2 can call the getName1() method to get the value of your variable.

Jody



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us