1

I am having this code in which I have created an object of class namely MyClass in it. I have created an object inside main method. I want to call the method of class but it gives StackovetrflowError at run time. Suggest me the way to overcome the error.

here is the code...

class MyClass {


  public MyClass obj2  =new MyClass();


    public void show()
    {
        System.out.println("in show method...");
    }

    void message()
    {
        System.out.println("in message method...");
    }
}


public class AccessDemo {

    public static void main(String[] args) {

         MyClass obj1  = new MyClass();

            obj1.obj2.show();
    }
}

I want to get the message printed inside the methods namely show() and message().

1
  • there is no method display()!!! Commented Feb 3, 2015 at 5:22

3 Answers 3

6

You've got needless recursion going on here:

class MyClass {

    public MyClass obj2 =new MyClass();

Why are you creating a MyClass instance inside of MyClass? This will repeat endlessly and recursively until you run out of memory.

Solution: don't do this. Get rid of the obj2 variable as it serves no purpose other than to make your program fail. Instead simply call your methods on the obj1 variable created in the main method.

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

2 Comments

"This will repeat endlessly and recursively until you run out of memory" needs a plus one do not you agree?
I posted an answer which in it I have mentioned your answer(just because of you have been mentioned the problem before me, otherwise I knew the problem also) and I gave another normal solution, if you are not agree with the mentioning please tell me to remove it. or even if you want I can remove my answer
1

as Hovercraft Full Of Eels described the problem is with : public MyClass obj2 =new MyClass(); but also you can have only public MyClass obj2; and not initialize it in the class scope, instead of it initialize it where it is needed, example:

class MyClass {

public MyClass obj2;

public void show()
{
    System.out.println("in show method...");
}

void message()
{
    System.out.println("in message method...");
}
//initializze it here
public void someVoid(){
   //you may do something else
   obj2=new MyClass();
}
} 

and now you can have:

public class AccessDemo {

public static void main(String[] args) {

     MyClass obj1  = new MyClass();

        obj1.someVoid();
        //now this should work properly
        obj1.obj2.show();
}
}

Output:

in show method...

Comments

0
obj1.obj2.show();

is incorrect. Instead you simply need to go:

obj1.show();
obj1.mesage();

2 Comments

yes that will work definately... what i wanted to check is that can i call a method using the way like obj1.obj2.show()? There is no compiler error in this... Is there any way to get the method invoked using this way or like this way...
@Ashishkumar There is nothing inherently wrong with obj1.obj2.show() [except for the fact that public components should be avoided--better would be obj1.getObj2().show()!!!] There's also nothing inherently wrong with a class having a field whose type is the same class. There are many use cases for this sort of thing--for example, a Person class might have a father field which is also a Person. That's why it's not a compiler error. It just isn't what you want here.

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.