-1

This code doesn't make sense for me

class MyClass{
    MyClass obj = new MyClass();

    public static void main(String[] args){
        MyClass obj = new MyClass();
    }
}

So the Question is how could class definition contain its own object creation? for example like above code MyClass instance variable obj creates its own object. here I want to understand if it works then how does it actually works behind the scene, And also what is the benefit of doing it this way .. detail explanation is needed .. thanks in advance

tumisma
  • 375
  • 3
  • 14
Abdul Raziq
  • 103
  • 6
  • 3
    it can easily, the problem is, you'll end up with an infinite amount of objects being recursively created, your resources will deplete soon and the application will crash. – Stultuske Mar 08 '16 at 09:36
  • 2
    you don´t benefit from this way, since you will be running into a `StackOverflow`, since each instance of `MyClass` will generate a new instance of `MyClass`. this infinite generation starts with your main. I guess you meant to have a `static MyClass obj` variable, which might be an indicator for a singleton class design. – SomeJavaGuy Mar 08 '16 at 09:36
  • so suppose we have two classes MyClass and MyClass2 . in MyClass2 is the main method where I create the object of MyClass .. will it also stackOverFlow ? – Abdul Raziq Mar 08 '16 at 09:40
  • 5
    @AbdulRaziq Just imagine what happens when you initialize the class `MyClass` currently. It has a variable `MyClass obj` which get´s initialized. It creates a new instance of `MyClass` for the `MyClass` that was just initialized. And what does this new instance do? it also hold a variable `MyClass obj` which get´s initialized aswell. This will continue until the jvm crashed with the current design. – SomeJavaGuy Mar 08 '16 at 09:43
  • This is a case of `stackOverFlow`, `java.lang.StackOverflowError` – Soumitri Pattnaik Mar 08 '16 at 09:46
  • @KevinEsche the confusion comes from here http://stackoverflow.com/questions/9780742/how-can-a-class-have-a-member-of-its-own-type-isnt-this-infinite-recursion – Abdul Raziq Mar 08 '16 at 09:50
  • @AbdulRaziq the second answer actually explains it pretty well what will happen. – SomeJavaGuy Mar 08 '16 at 09:54
  • 1
    @KevinEsche which second answer? They don't have an intrinsic order. – Erick G. Hagstrom Mar 08 '16 at 10:09
  • @ErickG.Hagstrom from Doug Swain – SomeJavaGuy Mar 08 '16 at 10:30
  • @KevinEsche ah yes, it does explain it well. – Erick G. Hagstrom Mar 08 '16 at 10:37
  • 1
    So @AbdulRaziq, I assume you're looking at that other question, the answer by Hovercraft Full Of Eels. He has a comment in that code, `// have fun!`. Did you try it? He did that so people could _see_ what happens. Did you see? – Erick G. Hagstrom Mar 08 '16 at 10:40
  • thanks @ErickG.Hagstrom – Abdul Raziq Mar 11 '16 at 17:03

2 Answers2

0

The thing is that the class doesn't differentiate whether you create the object of it's own kind or any other. Behind the scenes it works the same as any other object construction, so starts with the fields initialization, in this particular case, with the line:

MyClass obj = new MyClass();

And after that as people mentioned in comments it starts doing this over and over again, leading to the stack overflow.

kolakao
  • 71
  • 1
  • 9
-2

You don't initiate the MyClass inside your Myclass definition.

Get a look at this article from Bill Venners, Constructor Basics: http://www.javaworld.com/article/2076614/core-java/object-initialization-in-java.html

class MyClass{
    private int value;
    public MyClass() {
        value = 1234;
    }

    public MyClass(int setValue) {
        value = setValue;
    }
}

class Example3 {
    public static void main(String[] args) {
        // Create a MyClass object with default value
        MyClass example1 = new MyClass();
        // Create a MyClass with value 355
        MyClass example2 = new MyClass(355);
    }
}

I hope this helps.

DFerreira
  • 1
  • 2