313x Filetype PDF File size 0.52 MB Source: www.cs.iit.edu
Lecture Notes For Web Application using Java
VarunNalluri
CS 4VN
Computer Science Department
Illinois Institute of Technology
Chicago, Illinois 60616
vnalluri@hawk.iit.edu
Week :6Constructors
"Constructor is and initialization routine having same name as class name, which called automatically at
the time of object creation." In object-oriented programming, a constructor in a class is a special type
of subroutine called at the creation of an object. It prepares the new object for use, often accepting
parameters which the constructor uses to set any member variables required when the object is first
created. It is called a constructor because it constructs the values of data members of the class.
The purpose of the constructor is initialization followed by instantiation.
Constructor Rules:
a) The name of constructor must be same as class name.
b) The only applicable modifiers for the constructors are:
i) Public ii) protected iii) no accessibility iv) private
c) The constructor must not return any type not even void.
Example:
Public class Box{
Int width;
Int height;
Int depth;
public Box(int w, int h, int d){
width = w;
height = h;
depth = d;
}
}
Box b = new Box(1,2,3); //Instantiation
Lecture Notes For Web Application using Java
VarunNalluri
CS 4VN
Computer Science Department
Illinois Institute of Technology
Chicago, Illinois 60616
vnalluri@hawk.iit.edu
Constructors are either Default constructors or Parameterized constructors:
Reference type
Primitive type
Explicit default constructors
Implicit default constructor
Parameterized construcotrs
Default constructors
Constructors
no reviews yet
Please Login to review.