In this you learn about
--Constructors in Java
--Types of Constructors
1. Default Constructor
2. Parameterized Constructor
Difference between Constructor and Method
2. Topics for Today’s Session
Constructor
Difference between
Method and Constructor
Types of Constructors
3. Constructors
A constructor in Java is a special method that is used to
initialize objects.
The constructor is called when an object of a class is created.
It can be used to set initial values for object attributes
In Java, a constructor is a block of codes similar to the method.
It is called when an instance of the class is created.
At the time of calling constructor, memory for the object is
allocated in the memory.
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
4. Types of Java constructors
There are two types of constructors in Java:
1. Default constructor (no-arg constructor)
2. Parameterized constructor
Types
of
Constructors
Default Constructor
Parameterized
Constructor
5. A constructor that have no parameter is known as default
constructor.
Default constructor provides the default values to the object like 0,
null etc. depending on the type.
If there is no constructor in a class, compiler automatically creates a
default constructor.
Syntax :
<class_name>(){ }
6. // Create a MyClass class
class MyClass {
int n; // Create a class attribute
// Create a class constructor for the MyClass class
public MyClass() {
n = 10; // Set the initial value for the class attribute n
}
public static void main(String[] args)
{
MyClass myObj = new MyClass(); // Create an object of class MyClass
// ( This will call the constructor)
System.out.println(myObj.n); // Print the value of n
}
}
// Outputs
10
7. Example of default constructor that displays the default values
class Student{
int id;
String name;
void display()
{ System.out.println(id+" "+name);
}
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
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.
8. Parameterized constructor
Constructors that have parameters is known as 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
have two parameters. We can have any number of parameters in the
constructor. */
class Student {
int id;
String name;
Student (int i,String n){
id = i;
name = n;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[])
{
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
s1.display();
s2.display();
} }
9. Difference between constructor and method in java
There are many differences between constructors and methods.
Constructor is used to
initialize the state of an object.
Method is used to expose
behaviour of an 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 if you don't
have any constructor.
Method is not provided by
compiler in any case.
Constructor name must be
same as the class name.
Method name may or may
not be same as class name.
10. // Using Constructor
class Car {
int modYr;
String modName;
public Car(int y, String n) {
modYr = y;
modName = n;
}
public static void main(String[] args) {
Car myCar = new Car(1969, "Mustang");
System.out.println(myCar.modYr + " "
+ myCar.modName);
}
}
// Outputs
1969 Mustang
// Using Mathods
class Car {
int modYr;
String modName;
void getdata(int y, String n) {
modYr = y;
modName = n;
}
void show()
{
System.out.println(modYr);
System.out.println(modName);
}
public static void main(String[] args)
{
Car myCar = new Car( )
myCar.getdata(1969, "Mustang");
myCar.show();
}
}
// Outputs
1969
Mustang