0

I was just playing with final keyword and observed the below behavior, here i am assigning a final variable using a method and the method is getting called before the constructor

 public class Test {

    final int i=init(1);

    Test(){
        System.out.println("Inside Constructor");
    }

    public int init(int i){
        System.out.println("Inside Method");
        return i;
    }

    public static void main(String [] args){
        Test i=new Test();
        System.out.println(i.i);

    }

The Output of the following code is as below

Inside Method
Inside Constructor
1

I know final variable needs to be assigned before the constructors completes and this is what is happening here

What i am unable to find is that how can a method be called before a constructor, i really appreciate any explanation for this

1
  • Probably dup of Can I call methods in constructor in Java? as you are initializing your instance member during construction. Call to the constructor is only part of the construction. Commented Sep 11, 2016 at 8:45

3 Answers 3

3

It has nothing to do with finalkeyword. Try below(just removed final ) output will be same. Basically instance variable will be initialized first then constructor is called

public class Test {
     int i = init(1);

    Test() {
        System.out.println("Inside Constructor");
    }

    public int init(int i) {
        System.out.println("Inside Method");
        return i;
    }

    public static void main(String[] args) {
        System.out.println("start");
        Test i = new Test();
        System.out.println(i.i);
    }
}

Now why and how instance variable get initialized before constructor see Why instance variables get initialized before constructor called?

Sign up to request clarification or add additional context in comments.

3 Comments

yes i know that this will work without final,my bad i dint put in the question and from the given link i got that the compiler moves every initialization to every constructor but here since it is a method does this mean that method is called before construction ends , so far what i know is a object must me used for using a method
Yes, a method can be called before construction ends. Yes, this can result in screwy behavior if you're not careful.
i found out that a instance method can be called from constructor only after call to super constructor completes
2

If you change your constructor to

Test(){
    super();
    System.out.println("Inside Constructor");
}

and set a debug point to super(); you will see that the constructor gets called before the init(1);. It just gets called before your System.out.println("Inside Constructor");.

You can also write:

public class Test {
    final int i;

    Test(){
        super();
        i = init(1);
        System.out.println("Inside Constructor");
    }

    public int init(int i){
        System.out.println("Inside Method");
        return i;
    }

    public static void main(String [] args){
        Test i=new Test();
        System.out.println(i.i);

    }
}

Comments

1

This behaviour in the code is correct and has nothing to do with your analysis about the final key word...

init(1); is a method that is getting called as soon the class is constructing an instance...

there fore all inside the method will be executed even before the constructor...

1 Comment

init(1) will not be called as soon as class is loaded but when object is getting created

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.