The following code shows some examples of Constructors in Java.
For the purpose of initializing the instance variables of a class, we use constructors. Basically, a constructor is a special function that has the same name as that of the class. When we create an object, the constructor is called automatically. Furthermore, a constructor may or may not take parameters. A constructor without any parameter is called a default constructor. While the one with parameters is called the parameterized constructor.
Example of Default Constructor
The following code shows an example of a default constructor. The class Add is used to add two numbers by using a default constructor. It has two instance variables – first_number, and second_number. The default constructor takes two values as input from the user and displays their sum.
import java.util.*;
class Add{
int first_number, second_number;
public Add()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter First Number: ");
first_number=s.nextInt();
System.out.println("Enter Second Number: ");
second_number=s.nextInt();
System.out.println("Sum = "+(first_number+second_number));
}
}
public class Main
{
public static void main(String[] args) {
Add ob=new Add();
}
}
Output
Enter First Number:
678
Enter Second Number:
457
Sum = 1135
Example of Parameterized Constructor
The following code shows an example of the parameterized constructor. As can be seen, the class Triangle has two instance variables – base, and height. Further, it has a constructor that takes two parameters – base, and height. In order to distinguish between parameters and instance variables, we use this keyword. So, we use this keyword to refer to the instance variables. Meanwhile, the constructor also computes and displays the area of triangle.
class Triangle{
double base, height;
public Triangle(double base, double height)
{
this.base=base;
this.height=height;
System.out.println("Base of the Triangle: "+this.base);
System.out.println("Height of the Triangle: "+this.height);
System.out.println("Area of the Triangle: "+(0.5*this.base*this.height));
}
}
public class Main
{
public static void main(String[] args) {
Triangle ob=new Triangle(12.5, 8.3);
}
}
Output
Base of the Triangle: 12.5
Height of the Triangle: 8.3
Area of the Triangle: 51.87500000000001
Further Reading
- AI
- Android
- Angular
- ASP.NET
- Augmented Reality
- AWS
- Bioinformatics
- Biometrics
- Blockchain
- Bootstrap
- C
- C#
- C++
- Cloud Computing
- Competitions
- Courses
- CSS
- Cyber Security
- Data Science
- Data Structures and Algorithms
- Datafication
- Deep Learning
- DevOps
- Digital Forensic
- Digital Trust
- Digital Twins
- Django
- Docker
- Dot Net Framework
- Drones
- Elasticsearch
- Extended Reality
- Git
- Go
- HTML
- Image Processing
- IoT
- IT
- Java
- JavaScript
- Kotlin
- Latex
- Machine Learning
- MEAN Stack
- MERN Stack
- Microservices
- MongoDB
- NodeJS
- PHP
- Power Bi
- Projects
- Python
- Quantum Computing
- React
- Robotics
- Rust
- Scratch 3.0
- Shell Script
- Smart City
- Software
- Solidity
- SQL
- SQLite
- Tecgnology
- TypeScript
- VB.NET
- Virtual Reality
- Web Designing
- WebAssembly
- XML