2

This is my program:

public class Num2 {

   static
   {
        System.out.println("static block -1");
   }
   int no;
   Num2(int n)
   {
       no=n;
       System.out.println("Num");
   }
   Num2()
   {
        no=0;
        System.out.println("default num");
   }
   {
        System.out.println("Non-static block of num1");
   }
}



class call_by_value2 extends Num2
{
       static
       {
           System.out.println("static block of call by value");
       }  
       {
           System.out.println("non- static block of call by value");
       }
       call_by_value2()
       {   
          System.out.println("Default of call by value");
       }
       static void changeno(Num2 n1,Num2 n2)
       {
          Num2 n=new Num2();
          n=n1;
          n1=n2;
          n2=n;
       }
       public static void main(String[] args)
       {
          System.out.println("Main method");
          call_by_value2 c=new call_by_value2();
       }
 }

The output is:

static block -1
static block of call by value
Main method
Non-static block of num1
default num
non- static block of call by value
Default of call by value

which is desired output because we know that firstly non-static block is called then constructor.But when i modify this program and run it as this:

public class Num2 {

   static
   {
       System.out.println("static block -1");
   }
   int no;
   Num2(int n)
   {
       no=n;
       System.out.println("Num");
   }
   Num2()
   {
       no=0;
       System.out.println("default num");
   }
   {
       System.out.println("Non-static block of num1");
   }
}



class call_by_value2 extends Num2
{
      static
      {
          System.out.println("static block of call by value");
      }  
      {
          System.out.println("non- static block of call by value");
      }
      call_by_value2()
      {   
          super(50);
          System.out.println("Default of call by value");
      }
      static void changeno(Num2 n1,Num2 n2)
      {
          Num2 n=new Num2();
          n=n1;
          n1=n2;
          n2=n;
      }
      public static void main(String[] args)
      {
          System.out.println("Main method");
          call_by_value2 c=new call_by_value2();
      }
}

Now the output is:

static block -1
static block of call by value
Main method
Non-static block of num1
Num
non- static block of call by value
Default of call by value

So my question is if default constructor of callbyvalue2 class which contains super() method runs last how the parameterized constructor of Num2 class gives output before the output of default constructor of callbyvalue2 class?

earthmover
  • 4,395
  • 10
  • 43
  • 74
Fresher
  • 894
  • 1
  • 7
  • 27
  • 4
    This question is *much* more complicated than it needs to be, due to having two versions of the code, with the second version using messages which refer to class names from the first version, from irrelevant static blocks, from unconventional names and a lack of proper indentation. Please help us to help you by paying more attention to your question. – Jon Skeet May 29 '14 at 13:09
  • It's also unclear what the question has to do with the initializer blocks - your parameterless `call_by_value2` constructor chains to the parameterized `Num2` constructor as its first operation... why would you expect any *different* output? – Jon Skeet May 29 '14 at 13:11
  • @JonSkeet my question is simple that if `System.out.println("Default of call by value");` is the last line in output then `super(50);` should be the secondlast line of output because they are in same block.How come `super(50)` is giving output before non-static block of call by value? – Fresher May 29 '14 at 13:14
  • What does being in the same block matter? You're calling the super-constructor of `Num2`, which means it will execute any initializer blocks in `Num2`, then the constructor body of `Num2`. Again, you could make your question much, much *much* clearer through the use of good names, clear code, a single example, and no static initializers to confuse things. – Jon Skeet May 29 '14 at 13:17
  • @JonSkeet may be question is a bit messy.May be I am dumb.May be question is not clear but still i am unable to understand how the output is coming. – Fresher May 29 '14 at 13:23
  • It's not a matter of being dumb. It's about putting time in to asking a question as well as you can. If you can't be bothered to present the question clearly, why would you expect anyone to bother to present an *answer* clearly? – Jon Skeet May 29 '14 at 13:32
  • Basically it sounds like you should read section 12.5 of the JLS: http://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.5 – Jon Skeet May 29 '14 at 13:35

1 Answers1

4

No, this is what happens when you make a call such as.

new Num2() ;

1) All variables are initialised with default values (0, null, false, '\u0000') including those inherited from superclasses.

2) The implicit or explicit superclass constructor is called until the Object class (It is not allowed to pass any local variables or method returns as arguments to the superclass constructor invocation). Note that at this point although the implicit or explicit constructors are called they do not execute any code inside.

(if u have an empty constructor, the compiler will place an implicit call to the no-args superclass constructor, so it doesn't get to execute anything inside the method until top most superclass constructor is reached, since the first line of code in every constructor will be super() ; whether you put it or not. If the superclass does not have a no-args constructor available then u must explicitly provide one as the first line of code in your subclass constructor e.g. super("a string argument") ;)

3) When top level superclass constructor is reached, variables declared at that level are assigned with their initialisers (values assign explicitly e.g. int i = 23;), block statements execute before constructors no matter where they are declared in the class and constructors are executed after all non-static blocks execute.

Point 3 will then execute down the inheritance tree until Num2()

Example 1 This first example will not compile.

class Animal{
    Animal(int){

    }
}

class Dog extends Animal
{
    Dog(){
        // right here compiler will place an implicit call to super() ; not seen, at compile time.
        // since there is no no-args constructor available in class Animal compiler will issue a warning and stop compiling. Remember that a no-args constructor is provided by default ONLY if no other constructor was provided by the programmer. In this example we have a constructor in our superclass that takes an int as argument and so we must explicitly tell the compiler to call super(24);
    }
}

Example 2

class Animal{

}

class Mammal extends Animal{
    Mammal(int x){ 

    }
}

class Dog extends Mammal{
    Dog(){
        super(24) ;
    }

    Dog(int x){
       super(x) ;
    }
}

class BullDog extends Dog{
    BullDog(){
        this(24) ;
    }

    BullDog(int x){
        super(x) ;
    }
}

Now if we are to make an instance of BullDog using the no-args constructor the following constructor calls will happen.

1 BullDog() ;

2 BullDog(24) ;

3 Dog(24) ;

4 Mammal(24) ;

5 here although nothing seems to exist remember that compiler provides an implicit call to super constructor so here will be a call such as Anima();

6 here class Animal doesn't seem to have any constructor, however compiler will provide the following no-args constructor with a default call to super which should be the no-args constructor of class Object.

Animal(){
    super() ;
}

So 6 would be like Object() ;

You should now understand why code in any constructor won't execute until the Object constructor is reached and that is because there will alway be a call to the superclass constructor as first line. If you omit it the there better be a no-args constructor in your seperclass so that it works with what the complier provides by default otherwise you must explicitly declare it.

user9349193413
  • 1,203
  • 2
  • 14
  • 26
  • Sorry i didn't understand properly could you tell me how how come super(50) executed before default constructor of callbyvalue2 – Fresher May 29 '14 at 13:17
  • So firstly super() calls constructor of parent class then it executes any non-static block and then continues the execution of constructor. – Fresher May 29 '14 at 13:29
  • 1
    It won't execute any non-static block, until the constructor of the Object class is called. Then from top most parent class execution will shift to the non-static block/s found in Object class. Then what ever code is found in the constructor of class Object is executed. Then execution moves down the hierarchy and so non-static block/s found in next most child class are executed. Then what ever code is found in the constructor of that class is executed. – user9349193413 May 29 '14 at 14:11
  • 1
    Then execution moves even further dow the hierarchy and starts executing non-static block/s found further bellow in that class followed by what ever code was in that class's constructor and so on until the constructer that has been prepended with keyword new e.g. new BullDog() ; – user9349193413 May 29 '14 at 14:12