
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Getter and Setter in Java
Getter and setter are two special methods in Java that allow accessing and modifying data members' values. They are mostly used in encapsulation and data hiding to protect our sensitive data from unauthorized access. In encapsulation, we group related data and behavior together in a class and hide the implementation details from the outside world. Data hiding means preventing direct access of the members of class from the internal state of an object. In this article, we will explain what getter and setter methods are in Java and how they are useful in data hiding.
Getter and Setter methods
The Need
When we declare a class member as private then, it becomes non-accessible to any code outside its class, including its subclasses. This feature makes it more secure than other access modifiers like public and protected. By using getter and setter methods, we can control how the private fields of a class are accessed and modified. We can also add validation logic or other functionality in the getter and setter methods, such as checking whether the input is valid or not, we can validate the correct datatype for a specified data member and so forth.
Setter Method
These are public methods used to update the value of a private field. They usually have the prefix ?set' followed by the name of the field with first letter capitalized. It must have a void return type and take one parameter of the same type as the field it modifies.
Instance
public void setItemName( String name ) { this.itemName = name; }
Getter Method
These are public methods used to read the value of a private field. They usually have the prefix ?get' followed by the name of the field with first letter capitalized. It must have the same return type as the field it accesses.
Instance
public String getItemName() { return name; }
Java Program for Getter and Setter
Example 1
The following example illustrates the use of getter and setter method
Approach
Define three private members of a class.
Create two setter methods of type void. The first setter method is for ?itemName' and second for ?quantity'.
Now, create corresponding getter methods. The first getter method of return type String and the second of integer.
In the main() method, create an object of class ?Cart' and call the setter method along with an argument set item name. In the end, call the respective getter method.
public class Cart { // private members private String itemName; private int price; private int quantity; // first setter method public void setItemName( String itemName ) { this.itemName = itemName; } // first getter method public String getItemName() { return itemName; } // second setter method public void setPrice( int price ) { this.price = price; } // second getter method public int getPrice() { return price; } public static void main(String[] args) { Cart obj = new Cart(); // creating object obj.setItemName("Butter"); // setting item name obj.setPrice(50); // setting item quantity System.out.println("The details we have set are: "); System.out.println(obj.getItemName()); System.out.println(obj.getPrice()); } }
Output
The details we have set are: Butter 50
Example 2
This is another example of the getter and setter method with slightest change. We will provide our logic in setter method so that if the quantity of item is negative, the compiler will throw an error.
public class Cart { // private members private String itemName; private int price; private int quantity; // first setter method public void setItemName( String itemName ) { this.itemName = itemName; } // first getter method public String getItemName() { return itemName; } // second setter method public void setQuantity( int quantity ) { if(quantity <= 0) { System.out.println("Invalid input!"); } else { this.quantity = quantity; } } // second getter method public int getQuantity() { return quantity; } public static void main(String[] args) { Cart obj = new Cart(); // creating object obj.setItemName("Butter"); // setting item name // setting item quantity obj.setQuantity(-2); // wrong input obj.setQuantity(5); System.out.println("The details we have set are: "); System.out.println(obj.getItemName()); System.out.println(obj.getQuantity()); } }
Output
Invalid input! The details we have set are: Butter 5
Conclusion
We started this article by introducing getter and setter methods and in the next section, we have explained them in detail along with their need. In the end, we discussed their practical implementation through Java programs.