2

I have two classes :TestClass2 and TestClass3 . TestClass3 extends TestClass2.

    public class TestClass2 {
    static int i=10;
    static int k=TestClass3.j+100;
}


public class TestClass3 extends TestClass2{    
    static  int j=20;
}

Now when in another class when I print :

public static void main(String[] args) {
    System.out.println(TestClass3.j);
   System.out.println(TestClass2.k);
}

I get the result as 20 and 100

But when I just Print TestClass2.K (something like below)I get the result as 120.

 public static void main(String[] args) {
        System.out.println(TestClass2.k);
    }

Can someone please explain why this may be happening?

3 Answers 3

1

Let's see the following program in java.

public class Inheritance {
    static class TestClass2 {
        static int i=10;
        static int k=TestClass3.j+100;

        static
        {
            System.out.println("TestClass2 class : " + k);
            System.out.println("TestClass3 j : " + TestClass3.j);
        }

    }

    static class TestClass3 extends TestClass2{    
        static  int j=20;

        static
        {
            System.out.println("TestClass3 class : " + j);
        }


    }

    public static void main(String[] args) {
       System.out.println(TestClass3.j);
       System.out.println(TestClass2.k);
    }

}

The output is similar to :

TestClass2 class : 100
TestClass3 j : 0
TestClass3 class : 20
20
100

The TestClass2 will be initialized first when the TestClass3 is called which is inherited by the TestClass2.

As you can see the console message, we have an inheritance tree like this:

TestClass2 > TestClass3

So, the static/class variable j of the TestClass3 will be zero as a default value, so the output must be 100.

Comment the first line of code in the main method, then we can see the next result ;

TestClass3 class : 20
TestClass2 class : 120
TestClass3 j : 20
120

Both class variable k at TestClass 2 and j at TestClass 3 are initialized separately at this case. So, now we have 120 as an output.

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

Comments

0

If your Print(int a) method definition is just System.out.println(a) then the real result is 20 and 120. Because TestClass3 needs to be initialized before TestClass2. So TestClass3.j is 20. Then TestClass2 initializes k, which is 20 + 100, 120.

In order to help you more, please attach Print() method body too.

Comments

0

I'm sure that the tommybee's answer misses the point.

Case1:

public static void main(String[] args) {
    System.out.println(TestClass3.j);
    System.out.println(TestClass2.k);
}

When you print the TestClass3.j, before the initialization of TestClass3, you get the TestClass2 initialized, because it's the superclass of TestClass3. It means that the k in TestClass2 is initialized earlier than the j in TestClass3, and at that time the j is assigned with zero by default. So you get:

k = TestClass3.j + 100 = 0 + 100 = 100; 

and then back to TestClass3 to initialize the j by 20.

Case2

public static void main(String[] args) {
    System.out.println(TestClass2.k);
}

In this case, you print the TestClass2.k,and it runs the code in order.

static int k=TestClass3.j + 100;

You get TestClass3.j, which is initialized to be 20 (don't consider about k at this time, because k is on its way to grow), and then you make the k get 120.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.