Docstoc

Constructors - Macon State College

Document Sample
Constructors - Macon State College Powered By Docstoc
					  Constructors

“How Objects are Born”
             Constructors
• Constructors are special methods that are
  found in every object.
• The purpose of a constructor is to create
  objects (or ‘instantiate’ objects)
• An object’s constructor method is
  executed when the word ‘new’ is
  executed.
           Car myCar = new Car();
       Notes on constructors
• A constructor has the same name as the
  class
• All constructors return nothing (‘void’)
• Constructors should be defined as ‘public’
   More notes on Constructors
• If a constructor is not defined, one is
  automatically created by the compiler
  – you won’t see it in your source code
  – it will have no parameters
  – It will initialize all instance variables (the
    objects attributes) to their defaults
• If you create one constructor, java will not
  create the default constructor.
                           Example
public class Car
{                                          Notice this class
          private String VIN;              named ‘Car’.
          private int mileage, amtOfGas;
          public String getVIN()           It has three instance
          {
                    return VIN;
                                           variables; VIN,
          }                                mileage, amtOfGas.
          public int getMileage()
          {                                It has three methods;
                    return mileage;        getVIN, getMileage,
          }
          public int getAmtOfGas()
                                           and getAmtOfGas.
          {
                    return amtOfGas;
                                           It has no constructor.
          }
}
                             Example
public class TryThisCar
{
  public static void main(String args[])
  {
         Car myCar = new Car();
         System.out.println("My car's VIN is " + myCar.getVIN());
         System.out.println("My car's mileage is " +
         myCar.getMileage());
         System.out.println("My car has " +        myCar.getAmtOfGas()
  + " gas");
  }
}


Here is a program that uses my Car class to create an object named ‘myCar’.
                 Example
When my program, TryThisCar, runs, here is
the output:
  My car's VIN is null
  My car's mileage is 0
  My car has 0 gas

Notice the default constructor, which you
can’t see, set my instance variables to their
defaults.
                                Example 2
public class Car
   {
           private String VIN;                  This Car class has a
           private int mileage, amtOfGas;
           public Car(String inVIN)             constructor.
           {
                       VIN = inVIN;
           }                                    The constructor is expecting
           public String getVIN()               one parameter which is a String
           {                                    containing the VIN.
                                  return VIN;
           }
           public int getMileage()
           {
                       return mileage;
           }
           public int getAmtOfGas()
           {
                       return amtOfGas;
           }
   }
                           Example 2
public class TryThisCar
{
  public static void main(String args[])
  {
  Car myCar = new Car("1231239239482390");
  System.out.println("My car's VIN is " + myCar.getVIN());
  System.out.println("My car's mileage is " + myCar.getMileage());
  System.out.println("My car has " + myCar.getAmtOfGas() + " gas");
  }
}


Here is the program that uses the Car class with the new constructor. Notice
that when I create the new Car object, I have to provide a VIN because my
constructor (on previous slide) requires it.
               Example 2
The output from running this version looks
like this:
   My car's VIN is 1231239239482390
   My car's mileage is 0
   My car has 0 gas

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:2/3/2013
language:simple
pages:10