Java Constructors
A constructor in Java is similar to a method that is invoked when an object of the class is
created.
Unlike Java methods, a constructor has the same name as that of the class and does not
have any return type.
Types of Constructor
In Java, constructors can be divided into three types:
1. No-Arg Constructor
2. Parameterized Constructor
3. Default Constructor
1 Java No-Arg Constructors
Similar to methods, a Java constructor may or may not have any parameters (arguments).
If a constructor does not accept any parameters, it is known as a no-argument constructor. For
example,
private Constructor() {
// body of the constructor
}
[Link] constructor
public class Main {
int x; // Create a class attribute
// Create a class constructor for the Main class
public Main() {
x = 5; // Set the initial value for the class attribute x
public static void main(String[] args) {
Main myObj = new Main(); // Create an object of class Main (This will call the
constructor)
[Link](myObj.x); // Print the value of x
// Outputs 5
2. Example. Java Parameterized Constructor
class Main {
String languages;
// constructor accepting single value
Main(String lang) {
languages = lang;
[Link](languages + " Programming Language");
}
public static void main(String[] args) {
// call constructor by passing a single value
Main obj1 = new Main("Java");
Main obj2 = new Main("Python");
Main obj3 = new Main("C");
}
}
Output
Java Programming Language
Python Programming Language
C Programming Language
[Link] statement in Java Implementation a BankAccount class with constructors, deposit,
withdraw, and display functionalities
import [Link];
public class Bank {
static double balance = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int option = 0;
while (option != 4) {
[Link]("Welcome to the Bank of Java");
[Link]("1. Check Balance");
[Link]("2. Deposit");
[Link]("3. Withdraw");
[Link]("4. Exit");
[Link]("Enter an option: ");
option = [Link]();
switch (option) {
case 1:
checkBalance();
break;
case 2:
deposit();
break;
case 3:
withdraw();
break;
case 4:
exit();
break;
default:
[Link]("Invalid option. Try again.");
break;
}
}
}
public static void checkBalance() {
[Link]("Your current balance is $" + balance);
}
public static void deposit() {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the amount to deposit: $");
double amount = [Link]();
balance += amount;
[Link]("$" + amount + " has been deposited to your account.");
checkBalance();
}
public static void withdraw() {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the amount to withdraw: $");
double amount = [Link]();
if (amount > balance) {
[Link]("Insufficient funds.");
} else {
balance -= amount;
[Link]("$" + amount + " has been withdrawn from your account.");
}
checkBalance();
}
public static void exit() {
[Link]("Thank you for banking with us. Have a great day!");
}
}
4. A simple Java program demonstrating the implementation of a BankAccount class with
constructors, deposit, withdraw, and display functionalities
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
public class BankAccount {
private String DepositorName;
private long AccountNumber;
private String AccountType;
private double BalanceAmount;
// Default constructor
public BankAccount() {
DepositorName = " ";
AccountNumber = 0;
AccountType = " ";
BalanceAmount = -1;
}
// Parameterized constructor
public BankAccount(String dName, long accno, String accoType, double balAmount) {
DepositorName = dName;
AccountNumber = accno;
AccountType = accoType;
BalanceAmount = balAmount;
}
// Initialize method
public void initialize(String dName, long accno, String accoType, double balAmount) {
DepositorName = dName;
AccountNumber = accno;
AccountType = accoType;
BalanceAmount = balAmount;
}
// Display account details
public void display() {
[Link]("Depositor Name: " + DepositorName);
[Link]("Account Number: " + AccountNumber);
[Link]("Account Type: " + AccountType);
[Link]("Balance Amount: " + BalanceAmount);
}
// Deposit money
public void deposit(double amount) {
BalanceAmount += amount;
}
// Withdraw money if sufficient balance
public void withdraw(double amount) {
if (amount <= BalanceAmount) {
BalanceAmount -= amount;
} else {
[Link]("Insufficient balance!");
}
}
// Main method to test the class
public static void main(String[] args) {
BankAccount acc1 = new BankAccount();
[Link]("Ram Kumar", 120147852, "Saving", 8000);
BankAccount acc2 = new BankAccount("Piyush Kumar Singh", 1234567890,
"Current", 70000);
[Link](17000);
[Link]();
[Link]("**********************************");
[Link](20000);
[Link]();
}
}
Output:
Depositor Name: Ram Kumar
Account Number: 120147852
Account Type: Saving
Balance Amount: 25000.0
**********************************
Depositor Name: Piyush Kumar Singh
Account Number: 1234567890
Account Type: Current
Balance Amount: 50000.0