Home : Course Map : Chapter 3 : Java :
Class Constructors
JavaTech
Course Map
Chapter 3

Introduction
Class Definition
Data Fields
Methods
Constructors
Instantiation
  
Demo 1
  Demo 2
Static Members
  Demo 3

Value&Reference
  Demo 4
Overloading
   Demo 5
Wrappers 
  Demo 6
Autobox/Unbox
   Demo 7
Arrays
  Demo 8
Exceptions
Exercises

    Supplements
MoreAboutObjects
Creating Types

Classes,Objs&JVM

Java OOP vs C++
Garbage Collection
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The new operator creates an instance of a class as in, for example,

   ...
   int i = 4;
   Test test = new Test(i);
   ...

The statement declares a variable of the Test type and creates an instance of the class with the new operator. The argument of new must correspond to a special method in the class called a constructor.

The constructor signature looks similar to that of a regular method. It has a name that matches the class name and holds a list of arguments in parenthesis. However, a constructor has no return type. Instead, an instance of the class type itself returns.

Constructors are useful for initializing values and invoking any methods needed for initialization.

The code here shows a simple class with a constructor and one method.

class Test
{
  int i;


  Test(int j)
  {
    i = j;
  }

  int get()
  {
    return i;
  }
}





A constructor is called when an instance of this class is first created. Here it is used to initialize a variable.



The above code for the constructor ,

  Test(int j)
  {
    i = j;
  }

shows that in the process of creating an instance of the class, an inital value for the instance variable is passed as an argument in the constructor.

Java does not actually require an explicit constructor in the class description. If you do not include a constructor, the Java compiler will create a default constructor in the byte code with an empty argument. This default constructor is equivalent to the explicit  Test(){}

class Test
{
  int i;





  int get()
  {
    return i;
  }
}

A valid class with no constructor explicitly defined.

No constructor is defined so the JVM will use the default constructor:
   Test(){}

 

In the discussion of data fields, we noted that the data can receive explicit initial values or default values. You might wonder when does this initialization actually occur?

The java compiler, in fact, puts the initialization of the data into the byte code for the constructor. So, for instance, the above default constructor is equivalent to the one shown below where a constructor explicitly sets the the int variable to 0.

class Test
{
  int i;


  Test()
  {
    i = 0;
  }




  int get()
  {
    return i;
  }
}





This constructor illustrates explicitly the initialization of property values to their default values as would occur if we had just used
   Test(){}
or if no constructor had been defined.

Similarly, for a class with several types of data, the initialization of the default or explicit values occurs in the constructor:

class Test
{
  int i;
  boolean b;
  float f;

  double d = 1.1;

  Test()
  {
    i = 0;
    b = false;
    f = 0.0E0;
    d = 1.1;
  }

  int get()
  {
    return i;
  }
  ...
}








This
constructor illustrates explicitly the initialization of property values to their default values as would occur if we had just used
   Test(){}


You can define multiple constructors to provide optional ways to create and initialize instances of the class. (We will futher discuss overloading of constructors and methods later in this chapter.)

class Test
{
  int i;
  boolean b=true;
 

  Test(int j, boolean a)
  {
    i = j;
    b = a;
  }

  Test(int j)
  {
    i = j;
  }

  int get()
  {
    return i;
  }
}






This constructor initializes the two properties to the values passed as arguements.

A second constructor passes an initial value to just one of the variables.

Note that if a class includes one or more explicit constructors, the java compiler does not create the default constructor. So for the class shown above, the code

   
Test ex = new Test();

will generate a compiler error stating that no such constructor exists.

Latest update: Oct. 17, 2004

           Tech
OOP in Tech Apps
Complex Number
Histogram Class
  Demo
More Wrappers

           Physics
OOP in Physics
Particle Class
Root Finding
  Demo 1
Newton Methods
  Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.