N Sharma
N Sharma

Reputation: 34497

Is there any purpose to make private class variable to public

Hello I am curious to know that Is there any purpose to make private class variable to public in Java.

public class XYZ {

    public String ID;
    public ABC abc;

    private class ABC {
        public boolean isExist;
    }
}

Thanks in advance.

Upvotes: 1

Views: 541

Answers (4)

sga4
sga4

Reputation: 370

You can do either of the following two things to your class instance variables:

THING # 1: Keep your instance variables private. Then have public getter and setter methods to get and set the value of that variable. The good thing about it is that you get to put checks inside the setter method. For example, lengths can never be negative. So, you can't just make lengths public and let anyone assign it whatever value they want. You need to make sure the value being assigned to it is not negative. So:

class myClass {
    private int length;

    public void setLength(int i) {
        if ( i > 0 ) {
            length = i;
        }
    }
}

Also, you can make your instance variables read-only, write-only, or read-and-write, depending on the availability of getter and setter methods for that private variable.

THING # 2 : If you don't need any restrictions on the value of your instance variable, and you want it to neither be read-only nor write-only, then it's fine to keep that variable public. For example: babies can have any name - no restrictions:

class Baby {
    public name;
}

class Mother {
    public void nameTheBaby() {
        Baby baby = new Baby();
        baby.name = "Sarah";
    }
}

Upvotes: 0

codingbiz
codingbiz

Reputation: 26386

That is to make isExist visible to XYZ class.

Note, ABC is only visible to XYZ and not to any outside classes and its variable is public so you can have access to it. private has not meaning to XYZ, only outside classes

From inside XYZ,

ABC abc = new ABC(); //can only be accessed by XYZ.
abc.isExists = true; //can only be accessed by XYZ

Making isExist public means you do not care about encapsulating (prevent it from unwanted manipulation from outside) it. If you make it private, you will need a get accessor to expose it

private class ABC {
     private boolean _isExist; //only through accessors

     public boolean isExist()
     {
        return _isExist;
     }
}

Upvotes: 1

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95499

This is sometimes done for data-only classes. For example, this is sometimes done to represent the models stored in databases (see Objectify for a real example of how this is used, in conjunction with annotations, to represent the database models that are stored in an App Engine database).

That being said, this sort of thing makes for a very poor API. If you do this, I'd suggest doing it with classes that are either package-level access or in private nested classes, only. When exposing functionality or data to code outside your package, it is generally better to do it with a carefully designed interface that would allow you to change the implementation if your underlying structure were to change.

Upvotes: 1

peter.petrov
peter.petrov

Reputation: 39457

Yes, there's a purpose. If you do that then those program elements which can access the class can manipulate that variable directly. Otherwise (say if the variable is private), those elements would still be able to access the class but won't be able to manipulate the variable (unless you provide a getter/setter for it).

Think about it this way: the class modifier defines the level of access to the class, the variable modifier then defines the level of access to the variable itself (for those elements which can access the class).

Upvotes: 3

Related Questions