Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Java For Automation

  • What is Java?
  • Why to Learn Java?
  • History of Java
  • Types of Java Applications
  • Naming Conventions
  • Hello World Program
  • Internal Details of Java program
  • What is Java JDK, JRE and JVM ?
  • What is Java Comments ?
  • What is Java Variable?
  • Variable Naming convention
  • Primitive Data Types
  • Non Primitive Data Types
  • Type Casting
  • Java Arithmetic Operators
  • Relational Operators
  • What is logical operators in Java? 
  • What is Assignment Operators in Java? 
  • What is Unary Operators in Java? 
  • What is Ternary Operators in Java?
  • What is Java Control Statements?
  • What are Decision Making statements?
  • What are looping statements?
  • What are Jump statements?
  • What is OOPS concept in Java?
  • Advantages of Java OOPs
  • What is Java Inheritance?
  • What is Java Constructor?
  • What is this keyword?
  • What is Polymorphism?
  • What is super keyword ?
  • What is final keyword? 
  • What are access modifiers?
  • What is Encapsulation? 
  • What is Abstraction? 
View Categories
  • Home
  • Tutorials
  • Java
  • Java For Automation
  • What is Java Constructor?

What is Java Constructor?

What is Java Constructor?

A Java constructor is a special method used to initialize objects when they are created. It is called automatically when an object of a class is instantiated. Constructors have the same name as the class and do not have a return type, not even void.

Key Characteristics of Constructors: #

1. Name: A constructor must have the same name as the class in which it is defined.

2. No Return Type: Constructors do not have a return type, not even void.

3. Automatic Invocation: Constructors are automatically called when an object is created.

4. Overloading: Java allows constructor overloading, meaning a class can have more than one constructor with different parameter lists.

Types of Constructors: #

  1. Default Constructor:
    • Definition: A default constructor is a no-argument constructor provided by Java if no other constructors are defined. It initializes object fields with default values.

Example:

class Person {

    String name;

    int age;

    // Default constructor

    Person() {

        name = "Unknown";

        age = 0;

    }

}

public class Main {

    public static void main(String[] args) {

        Person p = new Person();

        System.out.println("Name: " + p.name); 

        System.out.println("Age: " + p.age);   

    }

}

Output:
Name: Unknown
Age: 0

  1. Parameterized Constructor:
    • Definition: A parameterized constructor allows initializing object fields with specific values provided during object creation.

Example:

class Person {

    String name;

    int age;

    // Parameterized constructor

    Person(String name, int age) {

        this.name = name;

        this.age = age;

    }

}

public class Main {

    public static void main(String[] args) {

        Person p = new Person("Alice", 30);

        System.out.println("Name: " + p.name); 

        System.out.println("Age: " + p.age);  

    }

}

Output:
Name: Alice
Age: 30



Constructor Overloading:

Java allows multiple constructors in a class, each with different parameter lists. This is known as constructor overloading.

Example:

class Person {

    String name;

    int age;

    // Default constructor

    Person() {

        name = "Unknown";

        age = 0;

    }

    // Parameterized constructor

    Person(String name, int age) {

        this.name = name;

        this.age = age;

    }

    // Another parameterized constructor with different parameters

    Person(String name) {

        this.name = name;

        this.age = 0;

    }

}

public class Main {

    public static void main(String[] args) {

        Person p1 = new Person();

        Person p2 = new Person("Bob", 25);

        Person p3 = new Person("Charlie");

        System.out.println("p1 Name: " + p1.name + ", Age: " + p1.age); 

        System.out.println("p2 Name: " + p2.name + ", Age: " + p2.age); 

        System.out.println("p3 Name: " + p3.name + ", Age: " + p3.age); 

    }

}

Output: 
p1 Name: Unknown, Age: 0
p2 Name: Bob, Age: 25
p3 Name: Charlie, Age: 0

Summary

Constructors are essential for initializing new objects in Java. They can be default or parameterized, and Java supports constructor overloading, allowing multiple constructors with different parameters to be defined in a single class.

Java
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
What is Java Inheritance?What is this keyword?
Table of Contents
  • Key Characteristics of Constructors:
  • Types of Constructors:
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105