What is an Instance?
Home ] Up ] What is a Class? ] [ What is an Instance? ] Class Members ] Methods and Overloading ] Instance Creation ] Instance Destruction ] Finalizers ] Class Variables and Methods ] Wrapper Classes ]

 

 

A class is a template from which to make an object

An instance of a class is an object.  (This is somewhat loose terminology, but the two terms are usually considered synonyms.)

  • Fido is an instance of class Dog
  • FredBloggs is an instance of class Person

For example, given a class like this:

class BankAccount
{
   // Instance methods
   double deposit(double amount)
   {
      m_balance += amount;
      return m_balance;
   }

   double withdraw(double amount)
   {
      m_balance -= amount;
      return m_balance;
   }

   String getHolder()
   {
      return m_holder;
   }

   // Instance variables
   String m_holder;
   double m_balance;
}

we can instantiate an instance of this class as follows:

BankAccount ac = new BankAccount();

The actual instantiation is performed in:

new BankAccount();

while:

BankAccount ac;

is just a reference to the instance.

The instance is created in two stages:

  1. The new operator creates the space for the class instance
  2. A constructor is called to initialize the instance

Note: More about constructor methods very soon...

 

This page was last modified on 02 October, 2007