Why do we declare Private variables in Java?

We Are Going To Discuss About Why do we declare Private variables in Java?. So lets Start this Java Article.

Why do we declare Private variables in Java?

  1. Why do we declare Private variables in Java?

    why are variables private in java
    To achieve encapsulation and this can't be accessible outside the class. This doesn't mean programmer can't change the source code.

  2. Why do we declare Private variables in Java?

    why are variables private in java
    To achieve encapsulation and this can't be accessible outside the class. This doesn't mean programmer can't change the source code.

Solution 1

When programmers talk about accessing a variable, they mean accessing its value when the program runs. Protecting the code from changes is another matter entirely and requires human processes rather than syntax of a programming language. Making a variable private “protects” its value when the code runs. At this level, we are not concerned with protecting it from other programmers changing the code itself. The point of so-called “data hiding” is to keep internal data hidden from other classes which use the class. Those other classes should only access behavior by calling methods on the class, not by changing values of variables directly.

General programming principles such as “data hiding” are followed to help us as programmers write correct code. If any class can change a variable’s value, then it is difficult to ensure that the value is valid. Say for example, you have a variable which counts the number of widgets a factory manufactures. By making the variable a private data member, you can more easily ensure that the value is never negative. On the other hand, if the variable is public, another class could change it to a negative value which can cause other parts of the code to crash.

Original Author Code-Apprentice Of This Content

Solution 2

why are variables private in java

To achieve encapsulation and this can’t be accessible outside the class. This doesn’t mean programmer can’t change the source code.

Original Author Ravi Of This Content

Solution 3

Variables are private to protect the state of your objects – in object-oriented programming terms, this is called encapsulation.

Here’s a very simple example. Imagine that we have a Person class, and a Person has an age that is calculated based on the year in which they were born.

class Person {

    private int yearOfBirth;
    private int age;

    public Person(int yearOfBirth) {
        this.yearOfBirth = yearOfBirth;

        this.age = Calendar.getInstance().get(Calendar.YEAR) - yearOfBirth;

    public int getAge() {
        return age;

In another class somewhere, we have this… and if age was public, we could really mess up the state of our object by changing it without updating the year of birth.

public static void main(String[] args) {
    Person bob = new Person(2000);

    System.out.println("Bob's age: " + bob.getAge());

    bob.age = 100;  //This would be BAD!

By encapsulating the age variable, it’s safe from unexpected changes and our class can manage its own state. Anyone who uses our class doesn’t have to care about calculating a person’s age, because that’s encapsulated within our class.

Original Author Riaan Nel Of This Content

Solution 4

“Private” variable means “controlled” access not “no” access. e.g. I can make the variable read-only by having only a getter method and no setter method. The owning class decides the access to to be provided to the variable – via methods it exposes to the public.

Also I can validate the value before storing it and reject values that are not allowed. I can also log the changes to the value.

It can also synchronize multiple variables so that it can be all in a consistent state e.g. doing debits and credits simultaneously.

And no – other people cannot change my code e.g if I provide my code as a compiled “jar” file. Or if they change it, and they break it – they own it (i.e. be responsible for the consequences their code change does).

An analogy from a real life would be room mates sharing expenses thru a shared wallet. If the wallet is public – anyone can take money from the wallet – no accountability. But let’s say one of the room mates (owning class) owns the wallet (private variable) – and provides a “getter” (you ask for money and I will give you from the shared wallet) to access the wallet – there is more accountability. No more anyone taking the money from the wallet will nilly. The keeper of the wallet can then log all access to it – in case of bugs (disputes) – to troubleshoot the problem. Similarly “addToWallet” method (room mates contributing to the wallet) can be used to add money to the wallet – again with more accountability as opposed to wallet lying in the open with any of the room mates adding
/ removing money from it willy nilly.

Original Author Ari Singh Of This Content


So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,


I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment