• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Class instanciation

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Test {
//This is where class fields are defined
Test t = new Test(); //Why is this not OK?
//This is where the constructors are defined
Test() {
Test t0 = new Test(); //OK
}
//This is where the methods are defined
void show(){
Test t1 = new Test(); //OK
}
public static void main(String args[]){
Test t2 = new Test(); //OK
}
}

I am still having hard time understanding why I can CREATE an OBJECT of THE CLASS ITSELF that is still BEING defined?
There is an analogy in Peter Van Der Linden's book that a class is like a RUBBER STAMP. If this analogy is taken literally, then creating an object of the class that is still being defined is like using the rubber stamp while it is still being carved (you will get different inprints of the rubber stamp at different time).
I know a analogy is only a tool for understanding, but in this case it gave me the wrong impression that a class definition has to be complete before an object of it can be created, which naturally leads to the wrong conclusion that a class can ONLY create objects of ANOTHER class but not of ITSELF.
Thanks
EC
[This message has been edited by Esenih Camai (edited January 09, 2001).]
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your analogy is OK, but like all analogies be careful of taking it too far. Software is a lot more flexible than things in the real world.
For example, if you had in mind a class 'Car', it's plainly nonsense to talk of a Car object containing another Car object, which might lead you to (incorrectly) reject the whole idea of classes containing other instances of themselves.
Alternatively, simply changing your mental picture from Car to Bag, it's immediately clear that one Bag can contain another. Therefore, you would want a computer language to allow for this.
So, to go back to generalities, it is essential that classes be allowed to make other instances of themselves. It's up to you as designer and programmer to use that feature of the language intelligently.
OK, onto your example.
Simple cases first - your 't1' and 't2' assignment.
These variables are inside method of the class. Therefore, each time the method is called, a new instance of the class is created. No problem.
So, why is the assignment to 't' disallowed? 't' does not blong to a method; It belongs to the class itself. Therefore every time an instance of Test is created, an instance of 't' will be created an assigned as well.
BUT ..... t is being assigned a new instance of Test. Therefore, that new instance will have to be created, which would have its own 't' to be created and assigned, which means a new instance of Test will have to be created ......
You get the picture. If your 't' assignment was allowed, it would lead to an implicit infinite loop - therefore, it's not allowed.
Hope this helps
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow Rob,
I was confused by the question.But your explanation was excellent. I am saving this on my PC.
Thanks,
SJ
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Esenih
I am trying to give an explanation for " Test t = new Test(); //Why is this not OK? ".
Any class can have either variables( int or char etc ) or constructors or methods.
Your code mentioned above does not belong to any one of the three things mentioned on the above line. It is something similar to floating statement which is not allowed.
This statement can be a part of a method or constructor but can not remain floating.
Regards
Sandip
 
Esenih Camai
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for sharing your knowledge.
EC
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you think about what that would do, you'd see why java doesn't allow this.
You create an object of Test
it creates an object of Test
which in turn creates an object of Test
and again this creates an object of Test
And another and another until The JVM runs out of memory!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!