Java

Reply

Join Date: Nov 2009
Posts: 2
Reputation: abalasuriya is an unknown quantity at this point 
Solved Threads: 0
abalasuriya abalasuriya is offline Offline
Newbie Poster

Java

 
0
  #1
23 Days Ago
  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,351
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 251
Moderator
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven
 
0
  #2
23 Days Ago
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 2
Reputation: abalasuriya is an unknown quantity at this point 
Solved Threads: 0
abalasuriya abalasuriya is offline Offline
Newbie Poster
 
0
  #3
22 Days Ago
  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 ?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,351
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 251
Moderator
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven
 
1
  #4
22 Days Ago
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.....
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 14
Reputation: shobhit123 is an unknown quantity at this point 
Solved Threads: 1
shobhit123 shobhit123 is offline Offline
Newbie Poster
 
0
  #5
21 Days Ago
Originally Posted by abalasuriya View Post
  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,351
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 251
Moderator
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven
 
-1
  #6
21 Days Ago
Originally Posted by shobhit123 View Post
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?
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 14
Reputation: shobhit123 is an unknown quantity at this point 
Solved Threads: 1
shobhit123 shobhit123 is offline Offline
Newbie Poster
 
-4
  #7
21 Days Ago
Originally Posted by masijade View Post
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..
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,182
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 481
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer
 
0
  #8
21 Days Ago
Originally Posted by shobhit123 View Post
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; 21 Days Ago at 5:42 am.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 14
Reputation: shobhit123 is an unknown quantity at this point 
Solved Threads: 1
shobhit123 shobhit123 is offline Offline
Newbie Poster
 
1
  #9
21 Days Ago
Originally Posted by peter_budo View Post
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 53
Reputation: MoZo1 is an unknown quantity at this point 
Solved Threads: 2
MoZo1 MoZo1 is offline Offline
Junior Poster in Training

sollution

 
0
  #10
20 Days Ago
This is a sollution for the 2nd question where the static has been taken out:

  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:
  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!
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC