• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Another Constructor Problem....

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Folks, in my quest to wrap my head around the idea of constrcutors, i've been neglecting my cattledrive assignments, big time.
Anyway, below is a small program taken from a book i've been reading/using.
Basically, in the code, what's the point of the constructor defined as:-
CountInstances()
{
CountInstances.addInstance();
}
I mean, what does it do? Why do we get what we do, if we comment out this portion of the code?
Below is the full listing.
Any input would be greatly appreciated.
Cheers in advance.
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure what your question is, but a constructor gets called whenever you do new SomeClassName(). In this case, the constructor is calling a static method within the class (so technically you wouldn't have to tack on the CountInstances to addInstance()) whenever new CountInstances() is called. This increments a static variable.
Constructor's are pretty easy, they're sort of like a normal method which is only called whenever a new instance of that class is created (otherwise known as an object).
[ May 26, 2003: Message edited by: jason adam ]
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Constructors are used with the keyword "new" in the creation of objects. They're often used to set up the initial state (assign values to fields) of an object. In Java, if you don't explicitly define a constructor, behind the scenes the compiler will insert a "default" constructor that takes zero arguments.
Consider the following code:

Since we didn't define a constructor, the compiler generated one like this behind the scenes:
public Steve()
{
}
The default constructor doesn't do much other than allow all the built-in behind-the-scenes object creation stuff to happen. In fact you can add this constructor explicitly to the given class and it won't change the behavior.
So what does this class "Steve" do anyhow? In the main() method, two objects of type "Steve" get created. One gets created at line (A) with the "new Steve()" operation, and a reference to this object gets stored in the variable s (we could use this variable "s" later in the main() method to access the Steve object it refers to, if we wanted to). A second Steve object gets created at line (B), but since no reference to it gets created, we can't do much with this object we created.
Since we're not doing anything very useful with these objects, let's prove to ourselves that they're getting created after all. Let's write an explicit constructor with some side-effect code -- code that announces that it's in the process of creating a Steve object. I'm calling this "side-effect" code because it isn't really playing a part in the setup of a Steve object; it's just printing a message that an object is in the process of getting created.

This code produces the output
A Steve object getting created.
A Steve object getting created.
Let's change our code a little bit now. Instead of printing out "A Steve object getting created." each time a Steve object gets created, let's just do a count of the objects as they get created, store that somewhere we can get to so we can print it out later. Remember that when you design a class there are pretty much two functions a class serves: (1) a class can serve as a template for creating individual objects (instances) of a class, and (2) a class can have "static" features that can work with all objects/instances of the class (or no instances of the class). Anyhow, a (private) static (class) field would be a sensible place to store the count of Steve objects getting created (and let's give the class a static method for accessing the count).

This class is now pretty similar to your CountInstances class. A few minor differences remain. CountInstances defines a method addInstance() that bumps up the count of instances, whereas we incremented our counter directly.
*****
To answer your original questions more directly:
SJ: I mean, what does it do? Why do we get what we do, if we comment out this portion of the code?
The CountInstances() constructor (as a side effect) calls the static (class) method addInstance(), which increments the static (class) variable numInstances, which keeps track of the number of CountInstance instances (objects) that are getting created. The main() method of CountInstances uses a loop to create 10 instances (objects) of CountInstance.
When you say "this portion" of the code how much do you mean?
If you mean just this much:
CountInstances()
{
// CountInstances.addInstance();
}
Well, the 10 objects are still getting created, but they're no longer being counted by this class because I've commented out some of the code that does the counting. (The printout says "Created 0 instances" but of course we know that's not true.)
If you comment out the entire constructor:
/*
CountInstances()
{
CountInstances.addInstance();
}
*/
Same thing. The 10 objects are still getting created (remember, the compiler has inserted a default constructor), but they're no longer being counted by this class and the printout saying "Created 0 instances" is not correct.
 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I think i'ver sussed it.


Cheers folks
I suppose i better get on and finish my Java Say (4a) assignment - taken me long enough so far!!
reply
    Bookmark Topic Watch Topic
  • New Topic