0

"Can anyone explain me execution flow for the following java code ??" Sorry for my misleading statement... my question is ...

Main b = new Main();

control goes to the A class constructer, then control directly goes to the overriden method of class Main

public void PrintFields() {
    System.out.println("x = "+x+" y = "+y);
}

Why compiler doesn't give error because x and y is not created..!! i am confused in this only.

When will x and y created in memory and when its initialization takes place ?? is the x and y created when control reaches to following statements in Main class ?

int x = 1;
int y;

Code:

class A
{
    public A() {
        PrintFields();
    }
    public void PrintFields() {}
}
class Main extends A
{
    int x = 1;
    int y;
    public Main() {
        y = -1;
        PrintFields();
    }
    public void PrintFields() {
        System.out.println("x = "+x+" y = "+y);
    }
}
class Test
{
    public static void main(String[] args) {
        Main b = new Main();
    }
}

Output:

run:
x = 0 y = 0 //why 0 0 ?
x = 1 y = -1 // why 1 -1 ?
BUILD SUCCESSFUL (total time: 0 seconds)

Thank you.

5
  • 7
    There's this thing called the debugger ... Mar 15, 2012 at 6:08
  • 2
    please don't down vote.. do tell your problems..
    – hardik
    Mar 15, 2012 at 6:08
  • Hardik this forum is to answer questions which you could not solve, as mentioned simply use debugger or do sysouts and you will understand the flow. Mar 15, 2012 at 6:15
  • @Hardik I have already posted answer before you edited your question. Now new question's answer is when you are creating an object first the memory is allocated, then the fields with their default values are loaded and then constructor is called. i hope now you got little bit of clear idea. Mar 15, 2012 at 6:38
  • @ChandraSekhar Thanx for your clear description.. Now i am getting idea. Thank you.
    – hardik
    Mar 15, 2012 at 6:49

3 Answers 3

5
Main b = new Main();

control goes to the A class constructer, then calls the overriden method of class Main

public void PrintFields() {
    System.out.println("x = "+x+" y = "+y);
}

At this time x and y have their default values

so x=0, y=0

Now control comes to

int x = 1;
int y;
public Main() {
    y = -1;
    PrintFields();
}

and then

public void PrintFields() {
    System.out.println("x = "+x+" y = "+y);
}

at this time x =1 and y=-1

so output is

x=1, y=-1
0
3

The flow is as follows:

  • main calls new Main().
  • the Main() constructor implicitly calls A() as the first thing.
  • A() calls PrintFields(). However, because this method is overridden, what actually gets executed is Main.PrintFields(). This prints the first line x = 0 y = 0 because the x and y fields have default values.
  • After A() completes, the Main() constructor continues to execute. It initializes x and y. (Note that the assignment part of int x = 1; is not executed until after A() completes.) Main() then calls PrintFields() which executes again and prints the (now initialized) values of x and y.

The description of how all this works can be found in the Java Language Specification (§12.5).

2
  • Thanx for the answer, my point is when control goes to overridden method and prints "x=0, y=0" but x an y is not created yet so how it can display "x=0, y=0".
    – hardik
    Mar 15, 2012 at 6:34
  • @Hardik - The variables have been created; they just have not been initialized (they have default values of 0). From the JLS link in my answer: "Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden." This happens before any constructors execute.
    – Ted Hopp
    Mar 15, 2012 at 6:40
0

If you change:

System.out.println("x = "+x+" y = "+y);  

to:

new Exception().printStackTrace(System.out); // <-- CFPMD (poor man's debugger)
System.out.println("x = "+x+" y = "+y);  

This might make it easier to understand what the other answerers are telling you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.