A Constructor is a block of code that initializes the newly created object. It is called when an instance(Object) of the class is created, it doesn’t have a return type. At the time of calling constructor, memory for the object is allocated in the memory. Constructor has same name as the class.
Note:
It is a special type of method which is used to initialize the object.
Every time an object is created using the new() keyword, at least one constructor is called.
Rules for creating Java constructor
Constructor name must be the same as its class name
A Constructor must have no explicit return type
A Java constructor cannot be abstract, static, final, and synchronized
A Java constructor cannot be abstract, static, final, and synchronized
Syntax:
public class MyClass
{
//This is the constructor MyClass()
{
//code
}
..
}
public class MyClass
{
//This is the constructor MyClass()
{
//code
}
..
}
Types of Java constructors:
There are two types of constructors in Java:
Default constructor (no-arg constructor)
There are two types of constructors in Java:
Default constructor (no-arg constructor)
Parameterized constructor
1.Default Constructor
A constructor is called "Default Constructor" when it doesn't have any parameter.
A constructor is called "Default Constructor" when it doesn't have any parameter.
Syntax:
class_name()
{
//code
}
Ex1:
Here, we are creating a no-arg constructor in the Bike class. It will be invoked at the time of object creation.
//Java Program to create and call a default constructor
class_name()
{
//code
}
Ex1:
Here, we are creating a no-arg constructor in the Bike class. It will be invoked at the time of object creation.
//Java Program to create and call a default constructor
class Bike1
{
//creating a default constructor Bike1()
{
System.out.println("Bike is created");
}
//main method
public static void main(String args[])
{
//calling a default constructor
Bike1 b=new Bike1();
}
}
Output:
Bike is created
Bike is created
Ex2:
class Student3
{
int id;
String name;
//method to display the value of id and name
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
//creating objects
Student3 s1=new Student3();
Student3 s2=new Student3();
//displaying values of the object
s1.display();
s2.display();
}
}
Output:
0 null
0 null
0 null
0 null
2.Parameterized Constructor
A constructor which has a specific number of parameters is called a parameterized constructor.
Ex:
class Student4
{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n)
{
id = i;
name = n;
}
//method to display the values
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
//creating objects and passing values
Student4 s1 = new Student4(111,"Ajay");
Student4 s2 = new Student4(222,"Vijay");
//calling method to display the values of object
s1.display();
s2.display();
}
}
Output:
I:\>javac Student4.java
I:\>javac Student4.java
I:\>java Student4
111 Ajay
222 Vijay
222 Vijay
** Find Error in the following example
class Example3
{
private int var;
public Example3(int num)
{
var=num;
}
public int getValue()
{
return var;
}
public static void main(String args[])
{
Example3 myobj = new Example3();
class Example3
{
private int var;
public Example3(int num)
{
var=num;
}
public int getValue()
{
return var;
}
public static void main(String args[])
{
Example3 myobj = new Example3();
System.out.println("value of var is: "+myobj.getValue());
}
}
}
}
No comments:
Post a Comment