[go: up one dir, main page]

0% found this document useful (0 votes)
8 views4 pages

Constructors

Uploaded by

Praveen Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Constructors

Uploaded by

Praveen Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Constructors

 Every class has a constructor.


 If the constructor is not defined in the class, the Java compiler builds a default
constructor for that class.
 While a new object is created, at least one constructor will be invoked.
 The main rule of constructors is that they should have the same name as the class.
 A class can have more than one constructor.
 Constructors are used for initializing new objects.
 Fields are variables that provide the state of the class and its objects, and methods are
used to implement the behaviour of the class and its objects.
Rules for writing Constructor
• Constructor(s) of a class must have same name as the class name in which it resides.
• A constructor in Java cannot be abstract, final, static and synchronized.
• Access modifiers can be used in constructor declaration to control its access i.e
which other class can call the constructor.
Following is an example of a constructor −
Example
public class myclass
{
public myclass()
{ // Constructor

}
public myclass(String name)
{
// This constructor has one parameter, name.
}
}
Types of constructors

There are two type of constructor in Java:

1. no-argument constructor:
 A constructor that has no parameter is known as default constructor.
 If the constructor is not defined in a class, then compiler creates default constructor
(with no arguments) for the class.
 If we write a constructor with arguments or no-argument then compiler does not
create default constructor.
 Default constructor provides the default values to the object like 0, null etc. depending
on the type.
Example1:Constructor
class Rectangle
{
int length;
int width;
Rectangle(int x,int y) //constructor method
{
length=x;
width=y;
}
int rectArea()
{
return(length*width);
}
}
class RectangleArea
{
public static void main (String args[])
{
Rectangle rect1=new Rectangle(15,10); //calling constructor
int area1=rect1.rectArea();
System.out.println(“Area =”+area1);
}
}

// Java Program to illustrate calling a no-argument constructor


import java.io.*;
class myclass
{
int num;
String name;
// this would be invoked while object of that class created.
myclass()
{
System.out.println(“Constructor called”);
}
}
class myclassmain
{
public static void main (String[] args)
{
// this would invoke default constructor.
myclass m1 = new myclass();
// Default constructor provides the default values to the object like 0, null
System.out.println(m1.num);
System.out.println(m1.name);
}
}
2. Parameterized constructor
 A constructor that has parameters is known as parameterized constructor.
 If we want to initialize fields of the class with your own values, then use
parameterized constructor.
/ Java Program to illustrate calling of parameterized constructor.
import java.io.*;
class myclass
{
// data members of the class.
String name;
int num;
// contructor with arguments.
myclass(String name, int n)
{
this.name = name;
this.num = n;
}
}
class myclassmain{
public static void main (String[] args)
{
// this would invoke parameterized constructor.
myclass m1 = new myclass(“Java”, 2017);
System.out.println(“Name :” + m1.name + “ num :” + m1.num);
}
}
 There are no “return value” statements in constructor, but constructor returns current
class instance.
 We can write ‘return’ inside a constructor.

constructor overloading
 Like methods, we can overload constructors for creating objects in different ways.
 Compiler differentiates constructors on the basis of numbers of parameters, types of
the parameters and order of the parameters.
// Java Program to illustrate constructor overloading
import java.io.*;
class myclass
{
// constructor with one argument
myclass (String name)
{
System.out.println(“Constructor with one “ + “argument - String : “ + name);
}
// constructor with two arguments
myclass (String name, int id)
{
System.out.print(“Constructor with two arguments : “ +” String and Integer : “ + name
+ “ “+ id);
}
// Constructor with one argument but with different type than previous.
myclass (long num)
{
System.out.println(“Constructor with one argument : “ +”Long : “ + num);
}
}
class myclassmain
{
public static void main(String[] args)
{
myclass m1 = new myclass (“JAVA”);
myclass m2 = new myclass (“Python”, 2017);
myclass m3 = new myclass(3261567);
}
}
Constructors are different from methods in Java

• Constructor(s) must have the same name as the class within which it defined while it
• void if does not return any value.
• Constructor is called only once at the time of Object creation while method(s) can be
called any numbers of time.

creating an Object
The class provides the blueprints for objects. The objects are the instances of the class. In
Java, the new keyword is used to create new objects.
There are three steps when creating an object from a class −
• Declaration − A variable declaration with a variable name with an object type.
• Instantiation − The ‘new’ keyword is used to create the object.
• Initialization − The ‘new’ keyword is followed by a call to a constructor. This call
initializes the new object

You might also like