Simple question about instantiating

Discussion in 'Java' started by Roy Gourgi, Oct 1, 2006.

  1. Roy Gourgi

    Roy Gourgi Guest

    Hi,

    I am a little bit confused with how OOP works with respect to instantiating.
    For example take the following code below. When each of the 10 c1 objects
    are instantiated from the CountTest() class, do they not each have their own
    instanceCount class variable??? It does not seem to be so from this code
    that I took from a book. In my thinking every time an object is created it
    initializes the instanceCount class variable to 0 and therefore the count
    would always be 1 after the constructor is run and the instanceCount is
    incremented. Can someone clarify what actually happens behind the scenes
    when an object is instantiated with the class variables.

    TIA

    Roy

    public class CountTestApp
    {
    public static void main(String[] args)
    {
    printCount();
    for (int i =0; i < 10; i++)
    {
    CountTest c1 = new CountTest();
    printCount();
    }
    }

    private static void printCount()
    {
    System.out.println("There are now " + CountTest.getInstanceCount() +
    " instances");
    }
    }

    class CountTest
    {
    private static int instanceCount = 0;
    public CountTest()
    {
    instanceCount++;
    }
    public static int getInstanceCount()
    {
    return instanceCount;
    }

    }
    Roy Gourgi, Oct 1, 2006
    #1
    1. Advertising

  2. Roy Gourgi

    Eric Sosman Guest

    Roy Gourgi wrote:

    > Hi,
    >
    > I am a little bit confused with how OOP works with respect to instantiating.
    > For example take the following code below. When each of the 10 c1 objects
    > are instantiated from the CountTest() class, do they not each have their own
    > instanceCount class variable??? It does not seem to be so from this code
    > that I took from a book. In my thinking every time an object is created it
    > initializes the instanceCount class variable to 0 and therefore the count
    > would always be 1 after the constructor is run and the instanceCount is
    > incremented. Can someone clarify what actually happens behind the scenes
    > when an object is instantiated with the class variables.
    >
    > TIA
    >
    > Roy
    >
    > public class CountTestApp
    > {
    > public static void main(String[] args)
    > {
    > printCount();
    > for (int i =0; i < 10; i++)
    > {
    > CountTest c1 = new CountTest();
    > printCount();
    > }
    > }
    >
    > private static void printCount()
    > {
    > System.out.println("There are now " + CountTest.getInstanceCount() +
    > " instances");
    > }
    > }
    >
    > class CountTest
    > {
    > private static int instanceCount = 0;
    > public CountTest()
    > {
    > instanceCount++;
    > }
    > public static int getInstanceCount()
    > {
    > return instanceCount;
    > }
    >
    > }


    instanceCount is a `static' variable, meaning that it
    belongs to the CountTest class as a whole and not to any
    particular instance of a CountTest object. To look at it
    another way, the single instanceCount (there is only one)
    is "shared" by all the CountTest objects (however many you
    decide to create).

    Since instanceCount belongs to the class it is initialized
    when the class is initialized. Since it does not belong to
    any particular object, it does not get re-initialized when
    an object is created. (The object should initialize all its
    parts, but instanceCount is not part of an object: it is part
    of the class.)

    However, the CountTest constructor is written in such a
    way that it increments instanceCount once each tim a CountTest
    object is constructed. This isn't really part of initializing
    the new CountTest, but a sort of side-effect. The constructor
    is doing two things: Initializing the new CountTest and also
    incrementing the single shared class variable.

    I hope this helps.

    --
    Eric Sosman
    lid
    Eric Sosman, Oct 1, 2006
    #2
    1. Advertising

  3. Roy Gourgi

    Roy Gourgi Guest

    Hi Eric,

    Is the constructor also only for the class (almost like a static method)
    that is run each time an object is instantiated? And if that is the case,
    then all the 10 objects that are created really have nothing in them because
    the class variable belongs to the class, the constructor also belongs to the
    class and finally also the public static int getInstanceCount() method?
    Correct me if I am wrong.

    One final question is in main there is the instantiation statement:

    CountTest c1 = new CountTest();

    because this statement is in a for loop that runs 10 times, would the c1
    reference variable not point to only the last object (i.e. 10th object)
    because each time that it is initialized it points to the last object?

    Thanks
    Roy

    "Eric Sosman" <> wrote in message
    news:...
    > Roy Gourgi wrote:
    >
    >> Hi,
    >>
    >> I am a little bit confused with how OOP works with respect to
    >> instantiating. For example take the following code below. When each of
    >> the 10 c1 objects are instantiated from the CountTest() class, do they
    >> not each have their own instanceCount class variable??? It does not seem
    >> to be so from this code that I took from a book. In my thinking every
    >> time an object is created it initializes the instanceCount class variable
    >> to 0 and therefore the count would always be 1 after the constructor is
    >> run and the instanceCount is incremented. Can someone clarify what
    >> actually happens behind the scenes when an object is instantiated with
    >> the class variables.
    >>
    >> TIA
    >>
    >> Roy
    >>
    >> public class CountTestApp
    >> {
    >> public static void main(String[] args)
    >> {
    >> printCount();
    >> for (int i =0; i < 10; i++)
    >> {
    >> CountTest c1 = new CountTest();
    >> printCount();
    >> }
    >> }
    >>
    >> private static void printCount()
    >> {
    >> System.out.println("There are now " +
    >> CountTest.getInstanceCount() + " instances");
    >> }
    >> }
    >>
    >> class CountTest
    >> {
    >> private static int instanceCount = 0;
    >> public CountTest()
    >> {
    >> instanceCount++;
    >> }
    >> public static int getInstanceCount()
    >> {
    >> return instanceCount;
    >> }
    >>
    >> }

    >
    > instanceCount is a `static' variable, meaning that it
    > belongs to the CountTest class as a whole and not to any
    > particular instance of a CountTest object. To look at it
    > another way, the single instanceCount (there is only one)
    > is "shared" by all the CountTest objects (however many you
    > decide to create).
    >
    > Since instanceCount belongs to the class it is initialized
    > when the class is initialized. Since it does not belong to
    > any particular object, it does not get re-initialized when
    > an object is created. (The object should initialize all its
    > parts, but instanceCount is not part of an object: it is part
    > of the class.)
    >
    > However, the CountTest constructor is written in such a
    > way that it increments instanceCount once each tim a CountTest
    > object is constructed. This isn't really part of initializing
    > the new CountTest, but a sort of side-effect. The constructor
    > is doing two things: Initializing the new CountTest and also
    > incrementing the single shared class variable.
    >
    > I hope this helps.
    >
    > --
    > Eric Sosman
    > lid
    Roy Gourgi, Oct 1, 2006
    #3
  4. Roy Gourgi

    Lew Guest

    (Top-posting re-arranged)

    > "Eric Sosman" <> wrote in message
    > news:...


    >> instanceCount is a `static' variable, meaning that it
    >> belongs to the CountTest class as a whole and not to any
    >> particular instance of a CountTest object. To look at it
    >> another way, the single instanceCount (there is only one)
    >> is "shared" by all the CountTest objects (however many you
    >> decide to create).

    -- snip --
    >> However, the CountTest constructor is written in such a
    >> way that it increments instanceCount once each tim a CountTest
    >> object is constructed. This isn't really part of initializing
    >> the new CountTest, but a sort of side-effect. The constructor
    >> is doing two things: Initializing the new CountTest and also
    >> incrementing the single shared class variable.


    Roy Gourgi wrote:
    > Hi Eric,
    >
    > Is the constructor also only for the class (almost like a static method)
    > that is run each time an object is instantiated? And if that is the case,
    > then all the 10 objects that are created really have nothing in them because
    > the class variable belongs to the class, the constructor also belongs to the
    > class and finally also the public static int getInstanceCount() method?
    > Correct me if I am wrong.


    The constructor does not belong to the class as static elements do, but
    constructs the individual instance. Constructors never "belong" to a method,
    static or instance. Check the definition of the keyword "static". Study the
    tutorials.

    > One final question is in main there is the instantiation statement:
    >
    > CountTest c1 = new CountTest();
    >
    > because this statement is in a for loop that runs 10 times, would the c1
    > reference variable not point to only the last object (i.e. 10th object)
    > because each time that it is initialized it points to the last object?


    Correct.

    I recommend Bruce Eckel's book, "Thinking in Java",
    http://www.mindview.net/Books/TIJ/

    Chapter 4 deals with constructors in detail.
    Check the index for the various references to the "static" keyword.

    - Lew
    Lew, Oct 1, 2006
    #4
  5. Roy Gourgi wrote:
    > Hi Eric,
    >
    > Is the constructor also only for the class (almost like a static method)
    > that is run each time an object is instantiated? And if that is the case,
    > then all the 10 objects that are created really have nothing in them because
    > the class variable belongs to the class, the constructor also belongs to the
    > class and finally also the public static int getInstanceCount() method?
    > Correct me if I am wrong.


    Every class, directly or indirectly, extends java.lang.Object, so every
    object, including each CountTest, has all the instance methods declared
    for Object. That said, as the name implies, a CountTest does not seem to
    have any use of its own. As the name implies, it appears to exist only
    to demonstrate the ability to count instance creation events.

    When a constructor runs it is associated with a particular instance, and
    "this" means the same as it would in a non-static method. In that way,
    it is more like a non-static method than a static method. On the other
    hand, unlike a static method, you don't need an existing instance of the
    class to invoke it.

    It is best to think of a constructor as just being a constructor,
    without the static/non-static distinction that applies to fields and method.


    >
    > One final question is in main there is the instantiation statement:
    >
    > CountTest c1 = new CountTest();
    >
    > because this statement is in a for loop that runs 10 times, would the c1
    > reference variable not point to only the last object (i.e. 10th object)
    > because each time that it is initialized it points to the last object?


    At least in theory, a c1 appears each time the loop body is executed,
    references the CountTest that was created by its initializer, and ceases
    to exist at the end of that execution of the loop body. The c1 that
    existed during the first iteration pointed to the first CountTest. The
    c1 that existed during the second iteration pointed to the second
    CountTest...

    Patricia
    Patricia Shanahan, Oct 1, 2006
    #5
  6. Roy Gourgi

    Roy Gourgi Guest

    Hi,

    The reason I was asking about the constructor was because of the class
    variable instanceCount that is initialized as:

    private static int instanceCount=0;

    I thought that class variables such as instanceCount could only be altered
    from static methods. Is that not the case?

    Thanks
    Roy



    "Lew" <> wrote in message
    news:...
    > (Top-posting re-arranged)
    >
    >> "Eric Sosman" <> wrote in message
    >> news:...

    >
    >>> instanceCount is a `static' variable, meaning that it
    >>> belongs to the CountTest class as a whole and not to any
    >>> particular instance of a CountTest object. To look at it
    >>> another way, the single instanceCount (there is only one)
    >>> is "shared" by all the CountTest objects (however many you
    >>> decide to create).

    > -- snip --
    >>> However, the CountTest constructor is written in such a
    >>> way that it increments instanceCount once each tim a CountTest
    >>> object is constructed. This isn't really part of initializing
    >>> the new CountTest, but a sort of side-effect. The constructor
    >>> is doing two things: Initializing the new CountTest and also
    >>> incrementing the single shared class variable.

    >
    > Roy Gourgi wrote:
    > > Hi Eric,
    > >
    > > Is the constructor also only for the class (almost like a static method)
    > > that is run each time an object is instantiated? And if that is the
    > > case,
    > > then all the 10 objects that are created really have nothing in them
    > > because
    > > the class variable belongs to the class, the constructor also belongs to
    > > the
    > > class and finally also the public static int getInstanceCount() method?
    > > Correct me if I am wrong.

    >
    > The constructor does not belong to the class as static elements do, but
    > constructs the individual instance. Constructors never "belong" to a
    > method, static or instance. Check the definition of the keyword "static".
    > Study the tutorials.
    >
    > > One final question is in main there is the instantiation statement:
    > >
    > > CountTest c1 = new CountTest();
    > >
    > > because this statement is in a for loop that runs 10 times, would the c1
    > > reference variable not point to only the last object (i.e. 10th object)
    > > because each time that it is initialized it points to the last object?

    >
    > Correct.
    >
    > I recommend Bruce Eckel's book, "Thinking in Java",
    > http://www.mindview.net/Books/TIJ/
    >
    > Chapter 4 deals with constructors in detail.
    > Check the index for the various references to the "static" keyword.
    >
    > - Lew
    Roy Gourgi, Oct 1, 2006
    #6
  7. "Roy Gourgi" <> writes:

    > I thought that class variables such as instanceCount could only be altered
    > from static methods. Is that not the case?


    No. A non-final variable can be altered wherever it is visible, and
    static variables are visible to both static and non-static (instance)
    code.
    Tor Iver Wilhelmsen, Oct 1, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nospam
    Replies:
    4
    Views:
    477
    Thomas Reinemann
    Jun 6, 2005
  2. Patrick Steele [MVP]
    Replies:
    1
    Views:
    492
    John Timney \(Microsoft MVP\)
    Jul 14, 2003
  3. Carl

    Instantiating a class in.NET

    Carl, Aug 5, 2003, in forum: ASP .Net
    Replies:
    9
    Views:
    396
    MS News \(MS LVP\)
    Aug 6, 2003
  4. Mike Fors
    Replies:
    2
    Views:
    342
    Mike Fors
    Aug 27, 2003
  5. asterixgallier

    Simple instantiating of class templates

    asterixgallier, Oct 8, 2006, in forum: C++
    Replies:
    4
    Views:
    354
    Salt_Peter
    Oct 8, 2006
Loading...

Share This Page