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

Pravin yadav
  • 567
  • 1
  • 6
  • 16
  • Probably dup of [Can I call methods in constructor in Java?](http://stackoverflow.com/questions/5230565/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. – SR_ Sep 11 '16 at 08:45

3 Answers3

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?

Community
  • 1
  • 1
M Sach
  • 33,416
  • 76
  • 221
  • 314
  • 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 – Pravin yadav Sep 11 '16 at 14:23
  • Yes, a method can be called before construction ends. Yes, this can result in screwy behavior if you're not careful. – Louis Wasserman Sep 11 '16 at 17:13
  • i found out that a instance method can be called from constructor only after call to super constructor completes – Pravin yadav Sep 16 '16 at 07:05
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);

    }
}
Lµk4s
  • 66
  • 7
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...

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97