public class Test {
  static Test t = new Test();
      public static void main(String[] args) {
     Test a = new Test();
       System.out.println(a.t);
    }}

This results stack overflow why

Recommended Answers

All 9 Replies

No it doesn't.

Does the real class maybe have a constructor?
And does that constructor maybe also call new on itself?
If so, what do think will happen if every call to new results in another to call new?

public class Test {
   Test t = new Test();
      public static void main(String[] args) {
     Test a = new Test();
       System.out.println(a.t);
    }}

Sorry!! I made a mistake code should be correct as above and then stack overflow results why ?

Because without the static, that Test t = new Test(); is an instance variable, that is instantiated with every call to new. So, you call new Test() in main, which triggers another new Test, which triggers another new Test, which triggers another new Test, which triggers another new Test, which trig.....

commented: good advice :) +4
public class Test {
  static Test t = new Test();
      public static void main(String[] args) {
     Test a = new Test();
       System.out.println(a.t);
    }}

This results stack overflow why

The code does not give any stack overflow error.
it runs fine.

The code does not give any stack overflow error.
it runs fine.

Which has already been covered. Did you read the thread or just knee jerk reply without bothering to find out what has gone before?

Which has already been covered. Did you read the thread or just knee jerk reply without bothering to find out what has gone before?

Yes i didnt see that properly.My mistake.
some people do think themselves smart using slang languages, you are indeed one of them.
anyways..

commented: Shouldn't have even responded. . -1
commented: Some people don't think before opening their mouths. -2

Yes i didnt see that properly.My mistake.
some people do think themselves smart using slang languages, you are indeed one of them.
anyways..

I do recommend you take time to read other responses as often people will reply to something that was already corrected and then wonder why they are hatted by masses. Flaming other and calling him "smart mouth" because he highlighted your mistake is bad idea, you would better do just saying "I'm sorry, my bad".
If you ignore previously provided feedback on this forum or real life you are more likely to fail on next step...

I do recommend you take time to read other responses as often people will reply to something that was already corrected and then wonder why they are hatted by masses. Flaming other and calling him "smart mouth" because he highlighted your mistake is bad idea, you would better do just saying "I'm sorry, my bad".
If you ignore previously provided feedback on this forum or real life you are more likely to fail on next step...

I do agree my mistake, but there is a way to point out the mistakes too,not simply saying something.We are all professionals.Throwing stones like this should not be done.
I accepted my mistake.
Anyways, lets not drag this issue more and carry on with some technical disscussion.

Thanks,
Shobhit

This is a sollution for the 2nd question where the static has been taken out:

public class Test {

    Test t = new Test();

    public static void main(String[] args) {
        try {
            Test a = new Test();
            System.out.println("meowmeow");
            System.out.println(a.t);
        } catch (Throwable e) {
            System.out.println("Error:");
            System.out.println(e.getClass());
            System.out.println(e.getMessage());
            System.out.println(e.getCause());
            e.printStackTrace();
        }
    }
}

So when you get stack overflow, you just have to look at the stack. The above code does this, and reports the error in a more convenient way. Also notice that I've cut apart that 2 line of code with a meowing, so now you can know where the error is coming from (before or after).
So now it's trivial, that when you created a new Test, it created a new Test for itself, which also created a new Test for itself, which...

Other fix than removing the redundancy altogether:

public class Test {

    Test t = null;

    public Test getTest() {
        if (this.t == null) {
            this.t = new Test();
        }
        return this.t;
    }

    public static void main(String[] args) {
        try {
            Test a = new Test();
            System.out.println("meowmeow");
            System.out.println(a);
            System.out.println(a.getTest());
            System.out.println(a.getTest().getTest());
            System.out.println(a.getTest().getTest().getTest());
        } catch (Throwable e) {
            System.out.println("Error:");
            System.out.println(e.getClass());
            System.out.println(e.getMessage());
            System.out.println(e.getCause());
            e.printStackTrace();
        }
    }
}

Lazy evaluation!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.