944,221 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 822
  • Java RSS
Nov 2nd, 2009
0

Java

Expand Post »
Java Syntax (Toggle Plain Text)
  1. public class Test {
  2. static Test t = new Test();
  3. public static void main(String[] args) {
  4. Test a = new Test();
  5. System.out.println(a.t);
  6. }}

This results stack overflow why
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abalasuriya is offline Offline
2 posts
since Nov 2009
Nov 2nd, 2009
0
Re: Java
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?
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 2nd, 2009
0
Re: Java
Java Syntax (Toggle Plain Text)
  1. public class Test {
  2. Test t = new Test();
  3. public static void main(String[] args) {
  4. Test a = new Test();
  5. System.out.println(a.t);
  6. }}

Sorry!! I made a mistake code should be correct as above and then stack overflow results why ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abalasuriya is offline Offline
2 posts
since Nov 2009
Nov 2nd, 2009
1
Re: Java
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.....
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 4th, 2009
0
Re: Java
Java Syntax (Toggle Plain Text)
  1. public class Test {
  2. static Test t = new Test();
  3. public static void main(String[] args) {
  4. Test a = new Test();
  5. System.out.println(a.t);
  6. }}

This results stack overflow why
The code does not give any stack overflow error.
it runs fine.
Reputation Points: 7
Solved Threads: 1
Newbie Poster
shobhit123 is offline Offline
14 posts
since Jun 2008
Nov 4th, 2009
0
Re: Java
Click to Expand / Collapse  Quote originally posted by shobhit123 ...
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?
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 4th, 2009
-4
Re: Java
Click to Expand / Collapse  Quote originally posted by masijade ...
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..
Reputation Points: 7
Solved Threads: 1
Newbie Poster
shobhit123 is offline Offline
14 posts
since Jun 2008
Nov 4th, 2009
-1
Re: Java
Click to Expand / Collapse  Quote originally posted by shobhit123 ...
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...
Last edited by peter_budo; Nov 4th, 2009 at 5:42 am.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 875
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Nov 4th, 2009
1
Re: Java
Click to Expand / Collapse  Quote originally posted by peter_budo ...
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
Reputation Points: 7
Solved Threads: 1
Newbie Poster
shobhit123 is offline Offline
14 posts
since Jun 2008
Nov 4th, 2009
0

sollution

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

JAVA Syntax (Toggle Plain Text)
  1. public class Test {
  2.  
  3. Test t = new Test();
  4.  
  5. public static void main(String[] args) {
  6. try {
  7. Test a = new Test();
  8. System.out.println("meowmeow");
  9. System.out.println(a.t);
  10. } catch (Throwable e) {
  11. System.out.println("Error:");
  12. System.out.println(e.getClass());
  13. System.out.println(e.getMessage());
  14. System.out.println(e.getCause());
  15. e.printStackTrace();
  16. }
  17. }
  18. }
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:
JAVA Syntax (Toggle Plain Text)
  1. public class Test {
  2.  
  3. Test t = null;
  4.  
  5. public Test getTest() {
  6. if (this.t == null) {
  7. this.t = new Test();
  8. }
  9. return this.t;
  10. }
  11.  
  12. public static void main(String[] args) {
  13. try {
  14. Test a = new Test();
  15. System.out.println("meowmeow");
  16. System.out.println(a);
  17. System.out.println(a.getTest());
  18. System.out.println(a.getTest().getTest());
  19. System.out.println(a.getTest().getTest().getTest());
  20. } catch (Throwable e) {
  21. System.out.println("Error:");
  22. System.out.println(e.getClass());
  23. System.out.println(e.getMessage());
  24. System.out.println(e.getCause());
  25. e.printStackTrace();
  26. }
  27. }
  28. }
Lazy evaluation!
Reputation Points: 43
Solved Threads: 4
Junior Poster
MoZo1 is offline Offline
110 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: help!
Next Thread in Java Forum Timeline: How to change application design? (custom jFrame, buttons)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC