Java Constructors
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java Constructors

Jeevadharshan

Java Constructors  

A constructor in Java is a special method used to initialize objects. It is automatically invoked when an object of a class is created and is commonly used to assign initial values to attributes (fields).

Key characteristics of constructors:

  • The constructor name must match the class name 
  • It has no return type (not even void) 
  • It runs automatically when new creates an object 
  • A class can have multiple constructors (overloading)

Default Constructor Example 

If you define a constructor without parameters, it initializes attributes with predefined values. 
 
public class Circle { 
    double radius; 
 
    // Constructor 
    public Circle() { 
        radius = 3.5; 
    } 
 
    public static void main(String[] args) { 
        Circle c = new Circle();  // constructor called 
        System.out.println("Radius: " + c.radius); 
    } 
}  

Output 

Radius: 3.5 

How Constructors Work

When new Circle() executes:
  1. Memory is allocated for the object 
  2. The constructor runs automatically 
  3. Attributes are initialized 
  4. The object reference is returned 

Parameterized Constructor 

Constructors can accept parameters to initialize objects with different values. 
 
public class Circle { 
    double radius; 
 
    // Parameterized constructor 
    public Circle(double r) { 
        radius = r; 
    } 
 
    public static void main(String[] args) { 
        Circle c = new Circle(5.0); 
        System.out.println("Radius: " + c.radius); 
    } 
}  

Output 

Radius: 5.0 

Constructor with Multiple Parameters 

You can initialize multiple attributes at once. 
 
public class Car { 
    int year; 
    String model; 
 
    public Car(int year, String model) { 
        this.year = year; 
        this.model = model; 
    } 
 
    public static void main(String[] args) { 
        Car car = new Car(2022, "Hyundai i20"); 
        System.out.println(car.year + " " + car.model); 
    } 
}  

Output 

2022 Hyundai i20  

Default vs User-Defined Constructors 

Every Java class automatically gets a default constructor (no parameters) if you don’t define one. However, once you create any constructor yourself, Java no longer provides the default automatically. 
 
public class Student { 
    String name; 
 
    // User-defined constructor 
    public Student(String name) { 
        this.name = name; 
    } 
 
Here, new Student("Ravi") is valid, but new Student() would cause a compile-time error. 

Constructor Overloading 

A class can have multiple constructors with different parameter lists. This is called constructor overloading. 
 
public class Box { 
    int width; 
    int height; 
 
    public Box() { 
        width = 1; 
        height = 1; 
    } 
 
    public Box(int size) { 
        width = size; 
        height = size; 
    } 
 
    public Box(int width, int height) { 
        this.width = width; 
        this.height = height; 
    } 
  
    public static void main(String[] args) { 
        Box b1 = new Box(); 
        Box b2 = new Box(5); 
        Box b3 = new Box(4, 6); 
 
        System.out.println(b1.width + "x" + b1.height); 
        System.out.println(b2.width + "x" + b2.height); 
        System.out.println(b3.width + "x" + b3.height); 
    } 
}  

Output 

1x1 
5x5 
4x6

Tags
Our website uses cookies to enhance your experience. Learn More
Accept !