0

I'm confused about this. Here is what I mean:

public class minMax {

    private int min = Integer.MAX_VALUE;

    public void changeMin() {
        min = 10;
    }

    public static void main (String args[]) {
        minMax ob1 = new minMax();
        ob1.changeMin();
        System.out.println(ob1.min); // outputs 10 <<<<
    }
}

So it outputs 10, even thought we didn't explicitly bind a min variable to the minMax constructor. I thought that you would have to have a constructor that initializes the object using the variable for this to work. e.g.:

    private int min;

    public minMax() {
        min = Integer.MAX_VALUE;
    }

What am I missing? Would appreciate some explanation, thank you!

EDIT:

To Illustrate my point further, why would we ever need to do the following:

private int min;

public minMax() {
    min = Integer.MAX_VALUE;
}

rather than leave it be the default constructor and just:

private int min = Integer.MAX_VALUE;

public minMax() {
}
Eli S.
  • 146
  • 1
  • 1
  • 10
  • Are you asking why the variable `min` contains the value that you actually assigned to it by explicitly calling the `changeMin()` method? I thought the question was about the fact that `Integer.MAX_VALUE` is assigned to `min` outside the constructor or other code block. Can you clarify what's the confusing part? – ernest_k May 25 '20 at 05:47
  • The variable is changed for that instance of the class (for the particular object). In our classes, we always initialized the variables inside the constructor of the class, and so I thought that this is what binds them. So I'm a bit confused, if we don't need to initialize it inside the constructor, then why ever do so? I will provide some code in my question to illustrate it. One sec. – Eli S. May 25 '20 at 05:59

3 Answers3

2

Changing the state (i.e. value) of instance variables (such as your min variable) doesn't have to take place in a constructor. It can take place in any method that has access to these variables, such as your changeMin() method.

EDIT: Your edit makes the question unrelated to the changeMin() method. Now it's about initialization of instance variables in a constructor vs. initialization in instance variable initializers. Both are valid ways to initialize instance variables. However, initialization in the constructor is more powerful, since it allows you to set the values of the instance variables to values passed to the constructor (as constructor arguments).

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Hey Eran, thanks for responding. Well, if we don't have to actually change the value of an instance variable inside a constructor, then in what scenario do we need to do it inside a constructor? In our course, we had to always initialize the variable inside the constructor, and so I always thought its a must. – Eli S. May 25 '20 at 05:57
  • 1
    @EliS.In the constructor you usually initialize the state of the instance variables to some initial values that bring the object into some valid state that makes the object usable. If that object is immutable, you can only set the values of the instance variables in the constructor. But if the object is mutable, you would usually have methods that change the value of instance variables. – Eran May 25 '20 at 06:02
  • But then, even if the object is immutable, couldn't you just, as illustrated in my object above, initialize the initial values that makes the object usable outside the constructor? I edited my question to illustrate my example – Eli S. May 25 '20 at 06:08
  • Your edit clarified and answered what my confusion was about. Thank you. – Eli S. May 25 '20 at 06:18
1

Here are the possibilities of class variable initialization in java:

Initialization on the go, lets call it default value

  • You don't have to repeat initialization in case of multiple constructors
  • Easy to read and understand how and where the variable is instantiated
private int min = Integer.MAX_VALUE;

Initialization in constructor

  • if variable value differs on in different constructor, then constructor initialization should be used
  • my preferred way of declaring variables, constructor is utilized the way it was designed
private int min;

public minMax() {
    this.min = Integer.MAX_VALUE;
}

public minMax(int min) {
    this.min = min;
}

Static block

  • Complex class-based initialization
  • After a class has been loaded and before any that class instances are created
private static int min;

static {
    min = Integer.MAX_VALUE;
}
Artyom Rebrov
  • 651
  • 6
  • 23
0

You are calling the main method from minMax Class. Inside the public changeMin() method you are assigning 10 to private min variable.changeMin() has access to private min as it is being called from same class

That private variable can be accessible from the same class it has been initialized. If you have a main method in another class and try to replicate this scenario you will get Error:(15, 31) java: min has private access in minMax.

That means if changeMin() method calls private min variable from some other class then that will be a compilation error

Read this for the better understanding about access modifiers.

Sarangan
  • 868
  • 1
  • 8
  • 24