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?