Class having static member of its own type

class ABC {
    int x;
    static ABC y = new ABC();
}


public class XYZ {

    public static void main(String[] args) {
        ABC q = new ABC();
        q.x = 5;
        System.out.println(q.x);
    }

}

Can someone explain what all happens (step by step) when the code is run.

Also, when the static keyword is removed before the instance y, StackOverflow error takes place. Why does this not happen when the static keyword is present?

Thanks in advance.

4 answers

  • answered 2017-06-17 19:55 AlmightyR

    static means that a single instance of the variable is used across multiple referencing objects (in this case, y).

    A static field lives in the context of the class, meaning all instances of the class will point to the same static object instance. In a scenario of:

    ABC a = new ABC();
    ABC b = new ABC();
    System.out.println(a.y==b.y);
    

    The print will result in true, because the instances of y on both a and b point to the same object.

    Non-static fields get populated with heir own individual instances; meaning that in a scenario of:

    ABC a = new ABC();
    ABC b = new ABC();
    System.out.println(a.y==b.y);
    

    (in a hypothetical scenario where such wouldn't trigger a StackOverflow) would result in false, because a.y would not be the same instance as b.y; each would have their own independent instances of ABC in their y fields.

    A StackOverflow will happen without the static keyword because when ABC is instantiated, it needs another ABC instance for it's y field, which, when instantiated, creates another ABC, and so on, infinitely (until the stack is overflown).

    With static, a single static instance of ABC, let's call it sABC is created, and it's own y field just references it itself, so the infinite loop of instantiation doesn't happen.

  • answered 2017-06-17 19:55 Rudi Angela

    When the class loader loads this class it will immediately create an instance of this class and assign it to the class variable y. This is because y is declared static. If you don't declare y static then you sayint that every instance of class ABC has an attribute y which is also an ABC. You are then also saying that the y attribute has to be assigned a new instance of class ABC, so this instance is created for you whenever you explicitly create an instance of ABC, as you do in your first statement in main(). Do you spot the recursion? That leads to a stack overfow.

  • answered 2017-06-17 19:55 J-Alex

    A static variable is a class-level variable.

    Instance variable is object-level variable.

    If you removing static keyword - this class trying instantiate itself and endless loop occurs, while static variable creates one instance one time and it's shared between all objects.

    Stack memory keeps local variables and function calls, so it's overflowed because you have endless calls.

  • answered 2017-06-17 19:55 Elan Hamburger

    The Walkthrough

    ABC q = new ABC();
    

    Create a new instance of the ABC class called q

    q.x = 5;
    

    Set q's property x to 5

    System.out.println(q.x);
    

    Print the value held by q's property x (should be 5)

    Static

    The static keyword means that every instance of a class shares that same variable. When you create an instance of the class ABC, it contains another instance of ABC as the variable y. When you create another instance of ABC, it also has a variable y, but that variable is the same as the y held by the first instance of ABC.

    If you were to remove the static keyword, y would not be shared by each instance of ABC. When you created an instance of ABC, it would create its own y, and that y would create its own y, and so on until you run out of memory and a StackOverflow error occurs.