Get and Set methods in Java

Image Credit - Pixabay

This lesson is about getter and setter methods in java which is a technique used as a part of programming language mechanism, encapsulation. First of all, let me tell you what is encapsulation.

According to wikipedia, a definition for the encapsulation is;

A language mechanism for restricting direct access to some of the object’s components.

Now, let me take the same previous Class, Student that we have created. Here, we defined some member variables and methods.


Now, as I mentioned before, if we used the public keyword to a method or a variable, it is visible to any outside class that imports the Student class. So, what’s the point??

The public interface of a Class includes all the public methods and variables. The public interface is the thing which is visible to the outside. To get a better idea of this concept, imagine this.

Toolkits in Java

Suppose some programmer is given a certain Java Class library. That is a collection of Java Classes used as a toolkit for developing something. For example, we use basic java.awt, javax.swing toolkits to develop graphical user interfaces. To import all the classes in those toolkits to our current working Class we use the import statement.


will import all the classes inside the swing toolbox.

(But those two toolboxes are somewhat outdated now. JavaFX and Qt Jambi are two of the currently used toolkits for java graphical user interface development)

OK back to the topic. As I was saying, you are given some library where you are prohibited to see the internal codes of the Classes in it, then how would you get the use of it??

This is the same thing that we undergo when we use any library. We don’t know how the Classes in javax.swing toolbox are written. They are really complicated and would perhaps take days to understand the implementation of the codes.

We actually, don’t need those serious details to use the classes. It’s similar to your computer. You don’t need to learn how the internal circuits in your computer work, in order to use it 🙂

So, in software toolboxes, you are given only the public interface of the Classes. That is, you actually need only public variables or public methods to achieve the purpose of the Class.

Then why private variables or methods are available? They are used only inside the class to do the implementation, behind the scene works… That means, they are not visible to the 3rd party programmer, but are just like a part of the hidden circuitry of the Class, which are required for the methods inside the class.

Review our Student Class. Now it is very important that the registrationNo and examMarks should not be directly accessible to a 3rd party. So, we can make the variables private.

What happens then?

First I’ll talk about the registrationNo.

Just think now we have imported the Student Class to another class called StudentDB. I’ll give you the full understanding about this import statement, but for now think we are using it like this;


Now can you understand how important this encapsulation is? You cannot modify this registrationNo after you have assigned a value for it from the constructor.

Error occurs when trying to access a private member variable of a Class from another Class

See, there’s an error if we call the registrationNo of st1 because, it is not visible to another Class.

Our one goal has reached. Now, a bad guy cannot change the registration number of the Student and make a mess!

But a new problem occurs. We cannot get the value of this registration number. So, something like,

will produce an error since it is not accessible. Now we are blind! How do we solve this??

Getter methods

Simple solution is available for the problem. Use a getter method for the variable. That is, you can make a method like getRegistrationNo() inside the Student Class that returns a copy of the registrationNo.

It is just a simple code: In the Student Class, type;


As registrationNo is visible inside the Class, the variable is accessible and the return statement will return a copy of the value, which can be assigned to a variable or used in a print statement of another Class like StudentDB.

Now this will work;

There’s one another problem that is still available. What happens if the person who enters the student details mistakenly type the registration number wrong? Then we cannot correct it, since the value of the private variable cannot be set outside the Class Student.

Setter Methods

Use a set method for that.


Yeh now it’s almost finished. Maybe you have realised that we have come again to the same problem. Now anyone can change this. So why do we waste our time to type the extra code of get and set methods? We can directly make the registrationNo public???

But how about this instead? In the Student Class;

Now everything may be solved! See the method is restricted! Only an authorized person can modify the registration number of a student 🙂

This is one the purposes of using get and set methods instead of making everything public. The security concerns. But in addition to that, you will understand why this is really important. This encapsulation will keep things neat.

Otherwise, everything will turn to a hell. It’s easy to make a mistake by modifying something that we are not supposed to do.

Just one line would be enough to make the whole program crash! So, better to use getter and setter methods for the variables that are not suitable to include for the public interface of your Class.

So, how do we decide whether a certain variable to be declared public or not?? Ah, some programmers think all member variables should be declared as private. Actually, it depends upon the programmer. With experience and according to the program, you will be able decide that!

Now I couldn’t finish the examMarks variable. You it the same way as the registrationNo 🙂

FYI, the programming language C# has a separate concern of these getter and setter methods. It has a special ‘variable’ kind of thing called “property” to handle this situation. We may also call examMarks and registrationNo as properties of the class Student.