Quantcast
Viewing latest article 5
Browse Latest Browse All 14

Answer by Ionut for Java setter and getter?

protected String name;public void setName(String newName){    if(newName.length() > 5) this.name = newName}public String getName(){    return this.name;}

In this simple case the name attribute can be accessed by its name in this class and in all its children. If you want to set the value of name from an unrelated class than you will have to use the setName() method where you can apply some validation for example.

Here you can find any information you need about this special methods.

Be aware that any property of a class can be accessed if the mutators and accessors are public. This is one of the key points of the Java Bean concept and almost all java frameworks relate to this concept at one point or another.


Viewing latest article 5
Browse Latest Browse All 14

Trending Articles