Building Constructors  
                             
A construct allowed initial values to be passed into the Point object so that x and y did not have to be set in
separate code.  


Point myPoint = new Point(2,5);


You can create your own constructors for classes you create.
       •        Constructors are special methods.
       •        Constructor methods have the same name as the class itself.
       •        Constructors look just like methods, but they have no return type, not even void.  
       •        Constructors are invoked only by using the new keyword, not the dot notation like methods.


public class Car{
 String color;
 String type;
 
Car(String c, String t){
   color = c;
   type = t;
 }

}


You should use constructors to initialize data. In this case, a constructor takes two Strings and initializes the
internal instance variables (color and type) using these passed-in values.

Use this constructor the same exact way the Point constructor was used.


Car wifeCar = new Car(“red”, “BMW”);


The constructor saves the extra step of initializing your data after instantiation of the object.

Behind the scenes, several things are actually done when you use the new keyword.
   •        1) “new” dynamically allocates space on the heap.










   •        2) The object is created and its instance variables are initialized with default values. More on default
values is coming up in a bit. In the meantime, know that
null is the default for String objects. Null represents
nothingness.










   •        3) Explicit initialization is executed. In the example above, neither color nor type are explicitly
initialized, but the color could have been defined with explicit initialization as follows:

String color = “black”;  //By default, the color or a car is black










   •        4) The last step is to call the constructor, thereby initializing the instance variables based on what the
constructor indicates.










What are the default values referred to in the second step? The default values differ based on the type of the
instance variable. The table below identifies the “default” for each of the types.





















Default Constructor

If you examine the first Car class code, the Car class did not have any constructors. However, you could still
call “new Car()” and get a Car object. What constructor got called?

The
Default constructor is a no-arguments constructor that is provided by Java if you define a class without
explicitly defining any constructors. The Default constructor allows you to create objects of classes that have
no specifically designed constructors. The Default constructor does not exist when the class contains any
other constructor.

One of the most common mistakes in Java is to rely on a Default constructor that no longer exists. Take, for
example, the following class definition:


public class Car{
 String color;
 String type;
 Car(String c, String t){
   color = c;
   type = t;
 }
}


Given this definition of the Car class, what is wrong with the following code?


Car wifeCar = new Car(“black”, “BMW”);
Car myCar = new Car();
myCar.type = “Sedan”;
myCar.color = “white”;


Do you see it? Will “new Car()” compile? No, it won’t, because there is no longer a constructor that takes no
arguments. The Default constructor was there until you provided the additional constructor Car(String,
String). To fix the above code, you could add the following constructor to your Car class.


Car(){}


This replacement puts a no-argument constructor back into the Car class.
Constructors
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials

Type

Default Value

Any Java Object

null

boolean

false

byte, short, int, long

0

float, double

0.0

char

‘\u0000’ or NUL/blank character
Services