| | |
Java
![]() |
•
•
Join Date: Nov 2009
Posts: 2
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
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
•
•
Join Date: Nov 2009
Posts: 2
Reputation:
Solved Threads: 0
0
#3 24 Days Ago
Java Syntax (Toggle Plain Text)
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 ?
1
#4 24 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.....
•
•
Join Date: Jun 2008
Posts: 14
Reputation:
Solved Threads: 1
0
#5 22 Days Ago
•
•
•
•
Java Syntax (Toggle Plain Text)
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
it runs fine.
-1
#6 22 Days Ago
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
----------------------------------------------
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
•
•
Join Date: Jun 2008
Posts: 14
Reputation:
Solved Threads: 1
-4
#7 22 Days Ago
•
•
•
•
Which has already been covered. Did you read the thread or just knee jerk reply without bothering to find out what has gone before?
some people do think themselves smart using slang languages, you are indeed one of them.
anyways..
0
#8 22 Days Ago
•
•
•
•
Yes i didnt see that properly.My mistake.
some people do think themselves smart using slang languages, you are indeed one of them.
anyways..
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; 22 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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Jun 2008
Posts: 14
Reputation:
Solved Threads: 1
1
#9 22 Days Ago
•
•
•
•
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 accepted my mistake.
Anyways, lets not drag this issue more and carry on with some technical disscussion.
Thanks,
Shobhit
•
•
Join Date: Mar 2008
Posts: 53
Reputation:
Solved Threads: 2
This is a sollution for the 2nd question where the static has been taken out:
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:
Lazy evaluation!
JAVA Syntax (Toggle Plain Text)
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 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)
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(); } } }
![]() |
Similar Threads
- FT Junior Java Developer Needed for Major Media firm in NYC (Software Development Job Offers)
- Java/J2EE Senior Software Engineer (Software Development Job Offers)
- Front-end Java Software Engineer for Digital Video/Media Market Leader (Software Development Job Offers)
- Senior Software Engineer (Java) (Web Development Job Offers)
- Java Software Engineer (Software Development Job Offers)
- Java Front-end Developer Engineer for Stealth Media Start-up (Software Development Job Offers)
- Java Developer Required (Software Development Job Offers)
Other Threads in the Java Forum
- Previous Thread: help!
- Next Thread: How to change application design? (custom jFrame, buttons)
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






