| Author |
can we create in the same class
|
Tomendra Singh
Ranch Hand
Joined: Jun 28, 2008
Posts: 47
|
|
hi all' i just want to know that can an object of any class can be created in the same class, if it is? please provide me how?
|
Strength Is Life,Weakness Is Death- Swamy Vivekanand.
SCJP 5.0(87%)
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
Not quite sure about your question, but the answer is probably "yes." You can have a linked list made up of nodes, each with a reference to the next node on the list. This is sometimes called a self-referential class.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
Just make sure you don't do that in the constructor (with the same parameters) or directly from the declaration of an instance field or you will get a StackOverflowError: If we change the first constructor to the following it will also go wrong: This is because in all examples documented as problematic, creating a new Test object results in the creating of a new Test object. Which results in the the creating of a new Test object. Which results in the the creating of a new Test object. This will continue until a StackOverflowError is thrown by the JVM.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: can we create in the same class
|
|
|