Getters and setters are methods that are used to access and modify the values of private instance variables of a class, respectively.
A getter method is used to retrieve the value of a private instance variable, and it typically has a name that starts with "get" followed by the name of the variable. For example, if a class has a private instance variable named "name", then the getter method for that variable could be named "getName()". A typical implementation of a getter method simply returns the value of the instance variable.
A setter method, on the other hand, is used to set the value of a private instance variable, and it typically has a name that starts with "set" followed by the name of the variable. For example, if a class has a private instance variable named "age", then the setter method for that variable could be named "setAge(int age)". A typical implementation of a setter method takes a parameter of the same type as the instance variable and sets its value to the instance variable.
By using getters and setters, you can control the access to the instance variables of a class, and you can enforce constraints on the values that are being set. For example, you could check if the value being set is within a certain range or if it meets certain conditions before actually setting it. This way, you can ensure that the state of your object remains consistent and valid.
//Base.java
public class Base {
private String name;
public void setName(String a) {
this.name = a;
}
public String getName() {
return name;
}
}
Our code defines a class named Base
that has a private instance variable name
of type String
. It also provides two methods to access and modify the value of this variable: a setter method setName()
and a getter method getName()
.
The setName()
method takes a parameter of type String
and sets the value of the name
variable to the value of the parameter. The getName()
method returns the current value of the name
variable.
The use of the private
access modifier for the name
variable ensures that it can only be accessed and modified within the Base
class. By providing public getter and setter methods, other classes can access and modify the value of the name
variable in a controlled way, without exposing the variable itself to direct modification from outside the class.
Note that the use of the this
keyword in the setName()
method is used to refer to the instance variable name
, which is otherwise shadowed by the method parameter a
of the same name.
//Main.java
public class Main {
public static void main(String[] args) {
Base someObj = new Base();
someObj.setName("Some Value");
System.out.println(someObj.getName());
}
}
In this case our code defines a class named Main
that has a main()
method. The main()
method creates a new object of type Base
named someObj
using the new
operator. It then calls the setName()
method on the someObj
object, passing it a string value "Some Value" as a parameter. This sets the value of the name
instance variable of the someObj
object to "Some Value".
Finally, the main()
method calls the getName()
method on the someObj
object and passes its return value to the println()
method of the System.out
object, which prints the value of the name
instance variable to the console.
This code demonstrates the use of getter and setter methods to access and modify private instance variables of an object in a controlled way. It also demonstrates the use of the new
operator to create new objects of a class, and the use of the System.out.println()
method to print output to the console.
No comments:
Post a Comment