prabh
prabh

Reputation: 396

Private Variables initialization

I ran the below code:

public class Box {

    private int length = 1;
    private int width = 2;
    private int height = 3;

     Box(int l, int w, int h) {
            length = l;
            width = w;
            height = h;
        }

        double volume() {
            return length * width * height;
        }

}

public class DemoBox {

    public static void main(String[] args) {

        Box box1 = new Box(3, 3, 3);
        System.out.println("Volumne: " +box1.volume());
    }
}

I always thought i wont be able to modify the values of private variables without getter/setter. But in the code above, i was able to pass values to private variables and the result volume came to be 27 (3*3*3). Is this expected behavior and pls explain where I am missing in my understanding in Private variables and also getters/setters

Upvotes: 0

Views: 632

Answers (7)

MariuszS
MariuszS

Reputation: 31567

All is fine, you CAN set this value this way (by constructor) only ONCE - on constructing object. After this you variables are safe, no one can change values.

Box is not object, this is class definition!

public class Box {
...
}

box1 is object, after construction there is no way to change it (without magic) :)

Box box1 = new Box(3, 3, 3);

If you want result of method value() to be 8, then you MUST define another object:

Box box2 = new Box(2, 2, 2);

After this box2.value() == 8.

There is no way to make box1.value() == 8 (without some magic).

But you can declare fields as final to protect declared values

private final int length = 1;
private final int width = 2;
private final int height = 3;

After this you cant modify his values even in constructor - compilation failure occurs.

Getter gives others permission to read value after creation time. Setter gives others permission to modify value after creation time. Without this your fields are safe.

Upvotes: 1

Prabhakar
Prabhakar

Reputation: 133

Private variables can be modified within the class where they are defined. From a class other than the one where the variables are defined we cannot modify private variables without setter.

Upvotes: 0

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

When you instantiate your member variables like

private int length = 1;
private int width = 2;
private int height = 3;

You're just providing defaults that can be overridden with different values passed through a constructor.

Box box1 = new Box(3, 3, 3);

But, then for this to make sense you also need to supply a default Constructor.

Box() {}

Without a default constructor there's no point in providing default values for your member variables as they would always get overwritten by your parameterized constructor.

Box defaultBox = new Box();
System.out.println("Volumne: " + box1.volume()); // prints 6; default volume

Now, the reason you were able to modify private variables is that you did so from within the constructor; a special class construct that initializes the state of the object being constructed. The private access modifier does not apply here because what you're protecting as private needs to be constructed first!

Upvotes: 0

Lucia Pasarin
Lucia Pasarin

Reputation: 2308

Yes, this is the expected behaviour. You are modifying the variables but inside the class they belong, in the way it is described inside the class. What you cannot do is get the variable like box1.length or set its value like box1.length = 5. It is like Box is the only one that can finally decide if you can get the variable value or set it and how. You can set length value from outside because Box "wanted it", because you allow it in Box constructor (or in another method, it would be the same case).

Upvotes: 0

user2161258
user2161258

Reputation: 43

private variables can be modified by the classes internals if they are not final. In your case the constructor of you class is overriding your default values. This is both valid and common.

Upvotes: 1

rgettman
rgettman

Reputation: 178253

Any code within the scope of the Box class can modify and/or read variables declared private within the scope of the Box class. In fact that is how getters and setters work; they are within the scope of the class and the access private variables.

When you say Box box1 = new Box(3, 3, 3); in DemoBox, the DemoBox code didn't modify the variables, the Box constructor did, and the Box constructor is within the scope of the class so it can access private variables.

Code in DemoBox cannot directly modify private variables in Box; any public methods in Box would allow DemoBox to modify them indirectly, through the method's code that is within the scope of the Box class.

Upvotes: 0

durron597
durron597

Reputation: 32323

You didn't pass them directly. You passed them in the Constructor, which is expected behavior. Note that the values are being changed in the code of the Constructor, which is contained in the class where these private fields are declared.

Try doing box1.length = 10 in the DemoBox class and see what your compiler says.

Upvotes: 1

Related Questions