Java Getter and Setter
Java Getter and Setter Example
Compile and Run
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Java Getter and Setter Example
class GetterAndSetter {
private String name;
// constructor
GetterAndSetter(String name) {
this.name = name;
}
// getter
public String getName() {
return this.name;
}
// setter
public void setName(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
// instance of client class
GetterAndSetter getterAndSetter = new GetterAndSetter("Rozgardesh");
System.out.println(getterAndSetter.getName());
}
}
Compile and Run
javac Main.java
javac Main
OutputRozgardesh
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Comments
Post a Comment