Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Constructors in Java

Constructors in Java are special methods that are used to initialize objects. Constructors are called when an object is created and can have parameters. There are default constructors generated by the compiler and parameterized constructors created by the programmer. Constructors initialize the state of the object and are similar to methods but cannot return values.

Uploaded by

Arijit Dutta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Constructors in Java

Constructors in Java are special methods that are used to initialize objects. Constructors are called when an object is created and can have parameters. There are default constructors generated by the compiler and parameterized constructors created by the programmer. Constructors initialize the state of the object and are similar to methods but cannot return values.

Uploaded by

Arijit Dutta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Constructors in Java

In Java, constructor is a block of codes similar to method. It is called when an instance of object is created and
memory is allocated for the object. It is a special type of method which is used to initialize the object.

When a constructor is called


Every time an object is created using new () keyword, at least one constructor is called. It is called a default
constructor.
The default constructor automatically initializes all instance variables to zero. The default constructor is often
sufficient for simple classes, but it usually won’t do for more sophisticated ones. Once you define your own
constructor, the default constructor is no longer used.

 A constructor has the same name as the class.


 A class can have more than one constructor.
 A constructor can take zero, one, or more parameters.
 A constructor has no return value.
 A constructor is always called with the new operator.

Note: It is called constructor because it constructs the values at the time of object creation. It is not necessary to
write a constructor for a class. It is because java compiler creates a default constructor if your class doesn't have
any.

Rules for creating java constructor


There are basically two rules defined for the constructor.
1. Constructor name must be same as its class name
2. Constructor must have no explicit return type

Types of java constructors


There are two types of constructors in java:
1. Default constructor (no-arg constructor)
2. Parameterized constructor

Java Default Constructor


A constructor is called "Default Constructor" when it doesn't have any parameter.
Syntax of default constructor:
<class_name>(){}

Example of default constructor


In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object
creation.
class Bike1{
Bike1(){
System.out.println("Bike is created");
}
public static void main(String args[]){
Bike1 b=new Bike1();
}
}
Output:
Bike is created
Rule: If there is no constructor in a class, compiler automatically creates a default constructor.
Q) What is the purpose of default constructor?
Default constructor is used to provide the default values to the object like 0, null etc. depending on the type.

Example of default constructor that displays the default values


class Student3{
int id;
String name;

void display(){
System.out.println(id+" "+name);
}

public static void main(String args[]){


Student3 s1=new Student3();
Student3 s2=new Student3();
s1.display();
s2.display();
}
}
Output:
0 null
0 null
Explanation: In the above class, you are not creating any constructor so compiler provides you a default
constructor. Here 0 and null values are provided by default constructor.

Java parameterized constructor:


A constructor which has a specific number of parameters is called parameterized constructor.

Why use parameterized constructor?


Parameterized constructor is used to provide different values to the distinct objects.

Example of parameterized constructor


In this example, we have created the constructor of Student class that has two parameters. We can have any
number of parameters in the constructor.
class Student4{
int id;
String name;

Student4(int i,String n){


id = i;
name = n;
}
void display(){
System.out.println(id+" "+name);
}
public static void main(String args[]){
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
s1.display();
s2.display();
}
}
Output:
111 Karan
222 Aryan

Constructor Overloading in Java


In Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods.
Constructor overloading in Java is a technique of having more than one constructor with different parameter
lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the
compiler by the number of parameters in the list and their types.

Example of Constructor Overloading


class Student5{
int id;
String name;
int age;
Student5(int i,String n){
id = i;
name = n;
}
Student5(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){System.out.println(id+" "+name+" "+age);}
public static void main(String args[]){
Student5 s1 = new Student5(111,"Karan");
Student5 s2 = new Student5(222,"Aryan",25);
s1.display();
s2.display();
}
}
Output:
111 Karan 0
222 Aryan 25
Difference between constructor and method in java
There are many differences between constructors and methods. They are given below.
Java Constructor Java Method
Constructor is used to initialize the state of an Method is used to expose behaviour of an object.
object.
Constructor must not have return type. Method must have return type.
Constructor is invoked implicitly. Method is invoked explicitly.
The java compiler provides a default constructor Method is not provided by compiler in any case.
if you don't have any constructor.
Constructor name must be same as the class Method name may or may not be same as class name.
name.
Java Copy Constructor
There is no copy constructor in java. But, we can copy the values of one object to another like copy constructor
in C++.
There are many ways to copy the values of one object into another in java. They are:
 By constructor
 By assigning the values of one object into another
 By clone() method of Object class
In this example, we are going to copy the values of one object into another using java constructor.
class Student6{
int id;
String name;
Student6(int i,String n){
id = i;
name = n;
}

Student6(Student6 s){
id = s.id;
name =s.name;
}
void display(){System.out.println(id+" "+name);}

public static void main(String args[]){


Student6 s1 = new Student6(111,"Karan");
Student6 s2 = new Student6(s1);
s1.display();
s2.display();
}
}
Output:
111 Karan
111 Karan

Copying values without constructor


We can copy the values of one object into another by assigning the objects values to another object. In this case,
there is no need to create the constructor.
class Student7{
int id;
String name;
Student7(int i,String n){
id = i;
name = n;
}
Student7(){}
void display(){System.out.println(id+" "+name);}

public static void main(String args[]){


Student7 s1 = new Student7(111,"Karan");
Student7 s2 = new Student7();
s2.id=s1.id;
s2.name=s1.name;
s1.display();
s2.display();
}
}
Output:
111 Karan
111 Karan

Q) Does constructor return any value?


Ans: yes, that is current class instance (You cannot use return type yet it returns a value).

Q) Can constructor perform other tasks instead of initialization?


Yes, like object creation, starting a thread, calling method etc. You can perform any operation in the constructor
as you perform in the method.

Calling another Constructor:


The keyword this refers to the implicit parameter of a method. However, the keyword has a second meaning. If
the first statement of a constructor has the form this(. . .), then the constructor calls another constructor of the
same class. Here is a typical example:
public Employee(double s)
{ // calls Employee(String, double)
this("Employee #" + nextId, s);
nextId++;
}

When you call new Employee(60000), then the Employee(double) constructor calls the Employee(String,
double) constructor. Using the this keyword in this manner is useful—you only need to write common
construction code once.

Initialization Blocks:
You have already seen two ways to initialize a data field:
● By setting a value in a constructor
● By assigning a value in the declaration

There is actually a third mechanism in Java; it's called an arbitrary blocks of code. These blocks are executed
whenever an object of that class is constructed. For example,
class Employee
{
public Employee(String n, double s)
{
name = n;
salary = s;
}
public Employee()
{
name = "";
salary = 0;
}
. . .
private static int nextId;
private int id;
private String name;
private double salary;
. . .
// object initialization block
{
id = nextId;
nextId++;
}
}

In this example, the id field is initialized in the object initialization block, no matter which constructor is used to
construct an object. The initialization block runs first, and then the body of the constructor is executed.
This mechanism is never necessary and is not common. It usually is more straightforward to place the
initialization code inside a constructor.

Static initialization block


If the static fields of your class require complex initialization code, use a static initialization block.
Place the code inside a block and tag it with the keyword static. Here is an example. We want the employee ID
numbers to start at a random integer less than 10,000.
// static initialization block static
{ Random generator = new Random();
nextId = generator.nextInt(10000);
}

Static initialization occurs when the class is first loaded. Like instance fields, static fields are 0, false, or null
unless you explicitly set them to another value. All static field initializers and static initialization blocks are
executed in the order in which they occur in the class declaration.

NOTE:
Here is a Java trivia fact to amaze your fellow Java coders: You can write a "Hello, World" program in Java
without ever writing a main
public class Hello
{
static
{
System.out.println("Hello, World");
}
}
When you invoke the class with java Hello, the class is loaded, the static initialization block prints "Hello,
World," and only then do you get an ugly error message that main is not defined. You can avoid that blemish by
calling System.exit(0) at the end of the static initialization block.

The this Keyword:


Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this
keyword. this can be used inside any method to refer to the current object. That is, this is always a reference to
the object on which the method was invoked. You can use this anywhere a reference to an object of the current
class’ type is permitted. To better understand what this refers to, consider the following version of Box( ):
// A redundant use of this.
Box(double w, double h, double d) {
this.width = w;
this.height = h;
this.depth = d;
}
This version of Box( ) operates exactly like the earlier version. The use of this is redundant, but perfectly
correct. Inside Box( ), this will always refer to the invoking object. While it is redundant in this case, this is
useful in other contexts, one of which is explained in the next section.

Instance Variable Hiding:


As you know, it is illegal in Java to declare two local variables with the same name inside the same or enclosing
scopes. Interestingly, you can have local variables, including formal parameters to methods, which overlap with
the names of the class’ instance variables. However, when a local variable has the same name as an instance
variable, the local variable hides the instance variable. This is why width, height, and depth were not used as
the names of the parameters to the Box( ) constructor inside the Box class. If they had been, then width would
have referred to the formal parameter, hiding the instance variable width. While it is usually easier to simply use
different names, there is another way around this situation. Because this lets you refer directly to the object, you
can use it to resolve any name space collisions that might occur between instance variables and local variables.
For example, here is another version of Box( ), which uses width, height, and depth for parameter names and
then uses this to access the instance variables by the same name:
// Use this to resolve name-space collisions.
Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
}
A word of caution: The use of this in such a context can sometimes be confusing, and some programmers are
careful not to use local variables and formal parameter names that hide instance variables. Of course, other
programmers believe the contrary— that it is a good convention to use the same names for clarity, and use this
to overcome the instance variable hiding. It is a matter of taste which approach you adopt.
Although this is of no significant value in the examples just shown, it is very useful in certain situations.

The finalize( ) Method:


Sometimes an object will need to perform some action when it is destroyed. For example, if an object is holding
some non-Java resource such as a file handle or window character font, then you might want to make sure these
resources are freed before an object is destroyed. To handle such situations, Java provides a mechanism called
finalization. By using finalization, you can define specific actions that will occur when an object is just about to
be reclaimed by the garbage collector.
To add a finalizer to a class, you simply define the finalize( ) method. The Java run time calls that method
whenever it is about to recycle an object of that class. Inside the finalize( ) method you will specify those actions
that must be performed before an object is destroyed. The garbage collector runs periodically, checking for
objects that are no longer referenced by any running state or indirectly through other referenced objects. Right
before an asset is freed, the Java run time calls the finalize( ) method on the object.
The finalize( ) method has this general form:
protected void finalize( )
{
// finalization code here
}
Here, the keyword protected is a specifier that prevents access to finalize( ) by code defined outside its class.
This and the other access specifiers are explained in Chapter 7. It is important to understand that finalize( ) is
only called just prior to garbage collection. It is not called when an object goes out-of-scope, for example. This
means that you cannot know when—or even if—finalize( ) will be executed. Therefore, your program should
provide other means of releasing system resources, etc., used by the object. It must not rely on finalize( ) for
normal program operation.

Overloading Methods:
In Java it is possible to define two or more methods within the same class that share the same name, as long as
their parameter declarations are different. When this is the case, the methods are said to be overloaded, and the
process is referred to as method overloading. Method overloading is one of the ways that Java implements
polymorphism. If you have never used a language that allows the overloading of methods, then the concept may
seem strange at first. But as you will see, method overloading is one of Java’s most exciting and useful features.

When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide to
determine which version of the overloaded method to actually call. Thus, overloaded methods must differ in the
type and/or number of their parameters. While overloaded methods may have different return types, the return
type alone is insufficient to distinguish two versions of a method. When Java encounters a call to an overloaded
method, it simply executes the version of the method whose parameters match the arguments used in the call.

Here is a simple example that illustrates method overloading:


// Demonstrate method overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for one integer parameter.
void test(int a) {
System.out.println("a: " + a);
}
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
// overload test for a double parameter
double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}
This program generates the following output:
No parameters
a: 10
a and b: 10 20
double a: 123.25
Result of ob.test(123.25): 15190.5625

Overloading Constructors:
In addition to overloading normal methods, you can also overload constructor methods. In fact, for most real-
world classes that you create, overloaded constructors will be the norm, not the exception. To understand why,
let’s return to the Box class developed in the preceding chapter. Following is the latest version of Box:
THE JAVA LANGUAGE
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
As you can see, the Box( ) constructor requires three parameters. This means that all declarations of Box objects
must pass three arguments to the Box( ) constructor. For example, the following statement is currently invalid:
Box ob = new Box();
Since Box( ) requires three arguments, it’s an error to call it without them. This raises some important questions.
What if you simply wanted a box and did not care (or know) what its initial dimensions were? Or, what if you
want to be able to initialize a cube by specifying only one value that would be used for all three dimensions? As
the Box class is currently written, these other options are not available to you. Fortunately, the solution to these
problems is quite easy: simply overload the Box constructor so that it handles the situations just described. Here
is a program that contains an improved version of Box that does just that:
// Here, Box defines three constructors to initialize the dimensions of a box various ways.
class Box {
double width;
double height;
double depth;
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
class OverloadCons {
public static void main(String args[]) {
// create boxes using the various constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
THE JAVA LANGUAGE
// get volume of cube
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
}
}
The output produced by this program is shown here:
Volume of mybox1 is 3000.0
Volume of mybox2 is -1.0
Volume of mycube is 343.0
As you can see, the proper overloaded constructor is called based upon the parameters specified when new is
executed.

Using Objects as Parameters


So far we have only been using simple types as parameters to methods. However, it is both correct and common
to pass objects to methods. For example, consider the following short program:
// Objects may be passed to methods.
class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
// return true if o is equal to the invoking object
boolean equals(Test o) {
if(o.a == a && o.b == b)
return true;
else
return false;
}
}
class PassOb {
public static void main(String args[]) {
Test ob1 = new Test(100, 22);
Test ob2 = new Test(100, 22);
Test ob3 = new Test(-1, -1);
System.out.println("ob1 == ob2: " + ob1.equals(ob2));
System.out.println("ob1 == ob3: " + ob1.equals(ob3));
}
}
This program generates the following output:
ob1 == ob2: true
ob1 == ob3: false
As you can see, the equals( ) method inside Test compares two objects for equality and returns the result. That
is, it compares the invoking object with the one that it is passed. If they contain the same values, then the
method returns true. Otherwise, it returns false. Notice that the parameter o in equals( ) specifies Test as its
type.
Although Test is a class type created by the program, it is used in just the same way as Java’s built-in types.
One of the most common uses of object parameters involves constructors. Frequently you will want to construct
a new object so that it is initially the same as some existing object. To do this, you must define a constructor that
takes an object of its class as a parameter. For example, the following version of Box allows one object to
initialize another:
// Here, Box allows one object to initialize another.
class Box {
double width;
double height;
double depth;
// construct clone of an object
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}JAVA LANGUAGE
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
class OverloadCons2 {
public static void main(String args[]) {
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
Box myclone = new Box(mybox1);
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
vol = mycube.volume();
System.out.println("Volume of cube is " + vol);
vol = myclone.volume();
System.out.println("Volume of clone is " + vol);
}
}
As you will see when you begin to create your own classes, providing many forms of constructor methods is
usually required to allow objects to be constructed in a convenient and efficient manner.

A Closer Look at Argument Passing:


In general, there are two ways that a computer language can pass an argument to a subroutine. The first way is
call-by-value. This method copies the value of an argument into the formal parameter of the subroutine.
Therefore, changes made to the parameter of the subroutine have no effect on the argument. The second way an
argument can be passed is call-by-reference. In this method, a reference to an argument (not the value of the
argument) is passed to the parameter. Inside the subroutine, this reference is used to access the actual argument
specified in the call. This means that changes made to the parameter will affect the argument used to call the
subroutine. As you will see, Java uses both approaches, depending upon what is passed.
For example, consider the following program:
// Simple types are passed by value.
class Test {
void meth(int i, int j) {
i *= 2;
j /= 2;
}
}
class CallByValue {
public static void main(String args[]) {
Test ob = new Test();
int a = 15, b = 20;
System.out.println("a and b before call: " + a + " " + b);
ob.meth(a, b);
System.out.println("a and b after call: " + a + " " + b);
}
}
The output from this program is shown here:
a and b before call: 15 20
a and b after call: 15 20

As you can see, the operations that occur inside meth( ) have no effect on the values of a and b used in the call;
their values here did not change to 30 and 10.
When you pass an object to a method, the situation changes dramatically, because objects are passed by
reference. Keep in mind that when you create a variable of a class type, you are only creating a reference to an
object. Thus, when you pass this reference to a method, the parameter that receives it will refer to the same
object as that referred to by the argument. This effectively means that objects are passed to methods by use of
call-by-reference. Changes to the object inside the method do affect the object used as an argument. For
example, consider the following program:

// Objects are passed by reference.


class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
// pass an object
void meth(Test o) {
o.a *= 2;
o.b /= 2;
}
}
class CallByRef {
public static void main(String args[]) {
Test ob = new Test(15, 20);
System.out.println("ob.a and ob.b before call: " + ob.a + " " +
ob.b);
ob.meth(ob);
System.out.println("ob.a and ob.b after call: " + ob.a + " " +
ob.b);
}
}
This program generates the following output:
ob.a and ob.b before call: 15 20
ob.a and ob.b after call: 30 10

As you can see, in this case, the actions inside meth( ) have affected the object used as an argument. As a point
of interest, when an object reference is passed to a method, the reference itself is passed by use of call-by-value.
However, since the value being passed refers to an object, the copy of that value will still refer to the same
object that its corresponding argument does.
When a simple type is passed to a method, it is done by use of call-by-value. Objects are passed by use of call-
by-reference.

Returning Objects:
A method can return any type of data, including class types that you create. For example, in the following
program, the incrByTen( ) method returns an object in which the value of a is ten greater than it is in the
invoking object.
// Returning an object.
class Test {
int a;
Test(int i) {
a = i;
}
Test incrByTen() {
Test temp = new Test(a+10);
return temp;
}
}
class RetOb {
public static void main(String args[]) {
Test ob1 = new Test(2);
Test ob2;
ob2 = ob1.incrByTen();
System.out.println("ob1.a: " + ob1.a);
System.out.println("ob2.a: " + ob2.a);
ob2 = ob2.incrByTen();
System.out.println("ob2.a after second increase: " + ob2.a);
}
}
The output generated by this program is shown here:
ob1.a: 2
ob2.a: 12
ob2.a after second increase: 22
THE JAVA LANGUAGE
As you can see, each time incrByTen( ) is invoked, a new object is created, and a reference to it is returned to
the calling routine.
The preceding program makes another important point: Since all objects are dynamically allocated using new,
you don’t need to worry about an object going out-of-scope because the method in which it was created
terminates. The object will continue to exist as long as there is a reference to it somewhere in your program.
When there are no references to it, the object will be reclaimed the next time garbage collection takes place.

You might also like