Encapsulation Java | Java Getters and Setters
Encapsulation Java is all about coming up with a nice intact setup to keep your data solidly wrapped inside methods. That’s where Java Getters and Setters come into picture as well.
You see nobody wants you to mess with their data. So, if you are working for FBI or even an institution, or client that doesn’t want their data secrets to leak out in the open, they would want you to make use of Encapsulation Java. They would want you grab hold of the data that you need from Java Getters and Setters methods without messing with other classes. That is considered to be one of the best ways of handling and working with data. Nothing gets out, you just get what you want. You deliver just the important bit to someone else using the program.
It is almost like Abstraction where we try to hide crucial information from our users.
Dumbed Down Example of Encapsulation Java
Just imagine data hiding or Encapsulation Java to be a scenario wherein you want to show your friend a picture of a monkey riding a bicycle, but you don’t want them to flip through all your pictures. Savvy?
So basically when you wrap data i.e. variables and the code that is acting on your data i.e. methods, together in one single unit so as to stop others from messing with your code, that’s what defines Encapsulation in Java.
Here are the top things that you need to do in order to achieve Encapsulation Java:
- Declare variables as private
- If you wish to modify a variable value, use a setter method.
- If you wish to retrieve a variable value, use a getter method.
Now we will see this with the help of a simple example and I am pretty sure things will become a hundred times more clear.
Advantages of Encapsulation Java
Doing so has its advantages. I have shot them in bullets below:
- You can make the fields in a class as read only or write only
- A class can be given complete control to decide which values need to be stored in fields
- Users of a class wouldn’t know the ‘how’ part.
- A user doesn’t have to bother about making unnecessary changes in the code.
- Achieving a powerful and intact system of data hiding.
Java Getters and Setters Example
We can achieve encapsulation Java by taking the aid of Java getters and setters.
Getters are nothing but getMethods() that you use to retrieve information. They are also known as accessors.
Setters are setMethods() that you use to edit the value of a variable. They are also known as mutators.
Here’s one easy example to help you understand.
I have created two variables namely gunCounts and name hereby which I have tried to use values from two setMethods() namely setCount() and setName(). In order to get the respective values from these variables I have used getCount() and getName().
Now to think like a user, I am creating a separate class with the main() method.
So the user, here me, has decided to call setCount() to enter a number of his choice. He has also used setName() method to enter a name of his choice. Then he is trying to display them using getCount() and getName() methods.
You will get the following result:
NOTE: For a Boolean variable you could make use of “is” as a prefix. So your methods could be:
Are Java Getters and Setters necessary?
Using Java Getters and Setters is nothing but a good habit of writing codes. It isn’t mandatory of course, unless you are working with JavaBeans. But it is highly recommended that you make use of Java Getters and Setters to deal with editing and retrieving data from variables. It makes structuring really easy and understandable.
If you are dealing with sensitive information and do not want your user to access your variables, you can choose to take the aid of Java Getters and Setters to implement Data Hiding.
So now onwards make it a wont to take assistance of these get and set methods whenever required, use appropriate return types and ensure the code is always user-friendly.