| Author |
Creating Own Object in Method
|
Debojit Sinha
Ranch Hand
Joined: Mar 13, 2010
Posts: 66
|
|
Hi, people,
Why is it legal to create object of own class from a method?
Thank You.
|
 |
Yogesh Gnanapraksam
Ranch Hand
Joined: Dec 17, 2009
Posts: 133
|
|
I don't understand your question.
Do you mean to say that it should not be allowed ?
|
 |
pankaj vijay
Ranch Hand
Joined: Apr 01, 2008
Posts: 75
|
|
Give me a reason why should be it illegal ???
|
Pankaj Vijay (SCJP, SCBCD)
Learn Core Java,Learn Servlet Jsp, SCJP Questions,Struts Tutorial
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12953
|
|
Do you mean something like this:
There's nothing wrong with creating any kind of object in a method, including an object of the class that the method is in. Why do you think it's strange that this is legal?
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Debojit Sinha
Ranch Hand
Joined: Mar 13, 2010
Posts: 66
|
|
Er, I think I phrased the question incorrectly, sorry.
My point was that since classes are created at compnile time, and, if a constructor to itself is called within it, should not here be an error? Since, after all, the class to be constructed is not in existence yet. In all probability I am wrong, and look forward to your clarification. Thank You.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
You mean something like this:
That will cause a StackOverflowError yes.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Debojit Sinha
Ranch Hand
Joined: Mar 13, 2010
Posts: 66
|
|
|
Sorry I took so long to answer, project lead was being a nazi, but thanks for the help.
|
 |
 |
|
|
subject: Creating Own Object in Method
|
|
|