简体   繁体   中英

Stack Overflow java

Object as a class variable causes the stackoverflow

public class stack {
        stack obj = new stack();   // its obvious that during class loading obj will call class to
        // load and infinite loop will occur. 
}

Lets say i am using static in from class obj

public class stack {
      static stack obj = new stack();  // it will not cause infinite loop and program will //execute successfully
}

Static variables are allocated in to the memory when the class is caught by JVM first time (As far I know). Say during first time only if the JVM starts allocating the memory to the above static object variable. It will intern call the class again and this should also cause infinite loop . Somewhere i am wrong. Can somebody highlight where i am wrong.

No, declaring it as static won't cause an infinite loop. Here is why.

Static variables are initialized during the class loading time. So when your class loads for the first time, compiler will create an instance for the static variable, and that's it. This won't cause your class to load a second time. Since your class is not loading again, this process won't be repeated.

If you declare it as a non-static attribute, then it's a totally different story. Consider this -

public class stack {
    stack obj = new stack();

    ........
}

This declaration is equivalent to -

public class stack {
   stack obj;

    public stack() {
        obj = new stack();    // implicitly moved here by the compiler
    }

    ........
}

From the last example, it's pretty obvious why there is an infinite recursion here. You are creating an instance of the stack class inside its own constructor, which in turn creates another, and then another,......and it goes on, resulting in a Stack Overflow.

Loading the "stack" class will induce creating an instance of "stack", saved as static field the stack class. Then, this instance of the stack class has nothing to load: No stack exception.

keyword Static has nothing to do with infinite loop. You can declare the field, method,class(static inner class)

Static Field :- Static fields are created and initialized when the class is first loaded. That happens when a static member of the class is referred to or when an instance of the class is created, whichever comes first.

Static method:- A method declared with the static keyword. Like static fields, static methods are associated with the class itself, not with any particular object created from the class. As a result, you don't have to create an object from a class before you can use static methods defined by the class.

The best-known static method is main, which is called by the Java runtime to start an application. The main method must be static, which means that applications run in a static context by default.

Memory Perspective:- As class is loaded once and its definition is stored in permgen area of jvm, static variables are also stored there and will lie for the life cycle of jvm.

Static variables are allocated in to the memory when the class is caught by JVM first time

Not exactly.

The static variables are allocated when the class is loaded.

The static variable initialization is performed when the class is initialized. That may happen some time after the class is loaded.

Say during first time only if the JVM starts allocating the memory to the above static object variable. It will intern call the class again and this should also cause infinite loop.

No. This is wrong for two reasons:

  • Objects in general are not "interned". Interning is an operation on String objects, and even then it only happens automatically for String literals. Non-literal String objects only get interned if your application calls String.intern() explicitly on them.

  • Even if some kind of interning was performed automatically, this would not cause an infinite loop. You seem to think that interning would somehow cause class initialization to be restarted or repeated. That cannot happen, the JVM goes to considerable lengths to ensure that each class is initialized at most once during its lifetime.

During debugging we can get to know , when first time control comes to the static variable , as variable is nothing but an instance of the class , it will call class loading .

then control enters in to the class , and finds a static variable object , but by this time it would be assigned by a memory address value by JVM(as done to other static variables). Control just ignores variable as instance and it assumes purely as static and program continues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM