Java Constructors


Hey all! I brought another tutorial on Java programming. When we talk about Object Oriented Programming, there are many concepts and topics to be spoken. So, this is such a topic. What are Constructors? Get ready to get to know about this topic mostly used when we are working with Java Classes.

Constructors

A constructor in Java is a block of code similar to a method that’s called when an instance of an object is created and memory is allocated for the object. How it differs from a method?
  • A constructor doesn’t have a return type.
  • The name of the constructor must be the same as the name of the class.
  • Unlike methods, constructors are not considered members of a class.
  • A constructor is called automatically when a new instance of an object is created.
If we don't include a constructor for a class, the java compiler explicitly adds the default constructor to each class.How to declare a constructor? This is it...


Whenever we create an object using Box class, the constructor is called by default. If we do not add it, constructor is called by default by the compiler, but there will not be any output.

Usage:

Default Constructors

We can use constructors to initialize attributes. Then the values set within the constructor will be assigned to the attributes. Look at this...

class Student{

int age;
String name;

Student(){
System.out.println("Student Constructor");
}
}

class Demo{
public static void main(String args[]){
Student s1=new Student();
System.out.println("Default age set by constructor: "+ s1.age);
System.out.println("Default name set by constructor: "+ s1.name);
}
}

You can see the attributes are not initialized at the beginning. But they have been initialized by the constructor. What will be the output?

In Java, default value of any variable or attribute is zero and default value of a string is null. That's why zero and null were printed to the console.

If you want to set a value for a variable whenever an object is created, you can initialize the variables in the constructor like this. From the above program...

class Student{

int age;
String name;

Student(){
age = 23;
name = "TechPool";
System.out.println("Student Constructor");
}
}

class Demo{
public static void main(String args[]){
Student s1=new Student();
System.out.println("Age initialized by constructor: "+ s1.age);
System.out.println("Name initialized by constructor: "+ s1.name);
}
}

Output:

We can include methods also in a constructor.  How to do it?

class Student{
// Declare attributes
int age;
String name;

Student(){
// Initialize attributes
age = 23;
name = "TechPool";
System.out.println("Student Constructor");
}

void displayDetails() {
// Values set by constructors are taken
System.out.println("Age = " + age);
System.out.println("Name = " + name);
}
}

class Demo{
public static void main(String args[]){
// create a Student object
Student s1=new Student();
// Method call
s1.displayDetails();
}
}

Output:

Parameterized Constructors

If we want, we can create a constructors with parameters. Then the parameters are assigned to the attributes to get a meaningful result.

class Cube{
//Attribute declarations
int length; //
int width; //
int height; //

// Parameterized constructor
Cube(int length, int width, int height){
this.length=length;
this.width=width;
this.height=height;
System.out.println("Constructor with parameters");
}

void printVolume(){
int volume; //Local variable
volume=length*width*height;
System.out.println("Volume is : "+volume);
}
}

class Demo{
public static void main(String args[]){
Cube c1=new Cube(10, 20, 30);
System.out.println("length of the box : "+c1.length);
System.out.println("width of the box : "+c1.width);
System.out.println("height of the box : "+c1.height);
c1.printVolume();
}
}


Output:


this keyword:
Used to refer the reference of the current object created. Whenever an object is created, this refers to the created object. So the parameters taken into the constructors are assigned to the attributes of class objects.

local variable:
The variables exist within a method. Their life time is limited to the method body. We can not call a local variable outside a method.

We can create more than one constructors. But remember! Their parameter list should be changed. There can not be two constructors within a constructor with the same signature and the parameter list. When we create with different parameters, Constructor Overloading comes to the party! Why? Same signature(name) but different parameter list! So, in the below program, first constructor is overloaded by the second.

Example:

class Cube{
//Attribute declarations
int length; //
int width; //
int height; //

// Constructor without parameters
Cube(){
System.out.println("Constructor without parameters");
}

// Constructor with parameters
Cube(int length, int width, int height){
this.length=length;
this.width=width;
this.height=height;
System.out.println("Constructor with parameters");
}

void printVolume(){
int volume; //Local variable
volume=length*width*height;
System.out.println("Volume is : "+volume);
}

}

class Demo{
public static void main(String args[]){
Cube c1=new Cube(10, 20, 30);
System.out.println("length of the box : "+c1.length);
System.out.println("width of the box : "+c1.width);
System.out.println("height of the box : "+c1.height);
c1.printVolume();
}
}

Output:

So, now I think you know the way to handle these constructors. It plays a major role in initialization of attributes of a class. Try these examples and get familiar with Java constructors..

Good Luck guys!





Admin Semoga Informasi Yang Diberikan Melalui Web Ini Dapat Bermanfaat Bagi Saudara/i

No comments for "Java Constructors"