• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor not called  RSS feed

 
Ninja Proctor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Nothing is printed. Why the constructor is not being called?
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are creating an array that holds Test objects. You are not creating the actual Test objects at this point.

Try this:



This creates a Test Array and initializes it with four Test objects. When you declare a collection of objects, you are just creating the collection object, not the objects that will be contained in the collection.

Hope this helps,
 
Jacquie Barker
author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sai Prasad kothapalle:


Nothing is printed. Why the constructor is not being called?


I like to refer to a Collection (such as an Array) as an "egg carton", with the objects it holds like the "eggs". (The analogy is a bit weak, since the "eggs" don't live in the "carton", but are rather referenced by the "carton".) Where the analogy is valid, however, is in saying that

Test[] t = new Test[4];

is creating an EMPTY egg carton. You must then individually create the "eggs" and put them into the carton:

Test t1 = new Test();
t[0] = t1;

etc.

Regards,

Jacquie
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!