Back to Course
Core Java

Java Constructors

What is a Constructor?

A constructor is a special method, matching the class name, that runs automatically when an object is created — typically used to initialize the object's fields.

public class User {
    String name;
    User(String name) {
        this.name = name;
    }
}
// used as: User u = new User("Asha");

Default Constructor

If no constructor is written, Java automatically provides a no-argument default constructor that does nothing beyond creating the object.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course