What is a constructor in Java, and how is it different from a method?
A constructor in Java is a special member of a class that has the same name as the class and no return type (not even void). It runs automatically when an object is created with the new keyword, and its job is to initialize the object's state. A method, by contrast, has its own name, must declare a return type, and is called explicitly to perform behavior.
Name and return type — a constructor must match the class name and cannot declare a return type; a method can have any legal name and must declare one, even if it is
void.Invocation — a constructor is invoked implicitly, exactly once per object, at creation time; a method is invoked explicitly and can be called any number of times.
Default constructor — if you define no constructor, the compiler supplies a no-arg default constructor with the same access level as the class; it never generates a body for any method you declare.
Overloading, not overriding — constructors can be overloaded and chained with
this()orsuper(), but they are not inherited and cannot be overridden,abstract,static, orfinal; methods can be all of these.Automation practice — in a Selenium Page Object class, the constructor receives the
WebDriverand callsPageFactory.initElements(driver, this), so every page object is fully initialized before any test uses it.
Key terms: constructor, default constructor, parameterized constructor, constructor overloading, constructor chaining,
this(),super(),newkeyword, return type, Page Object Model
public class ConstructorDemo {
private String browser;
// No-arg constructor - chains to the parameterized one
ConstructorDemo() {
this("chrome"); // must be the first statement
System.out.println("No-arg constructor finished");
}
// Parameterized constructor - same name as class, no return type
ConstructorDemo(String browser) {
this.browser = browser;
System.out.println("Parameterized constructor: " + browser);
}
// Regular method - has its own name AND a return type
String getBrowser() {
return browser;
}
public static void main(String[] args) {
ConstructorDemo d1 = new ConstructorDemo();
// Output: Parameterized constructor: chrome
// Output: No-arg constructor finished
ConstructorDemo d2 = new ConstructorDemo("firefox");
// Output: Parameterized constructor: firefox
System.out.println(d1.getBrowser()); // Output: chrome
System.out.println(d2.getBrowser()); // Output: firefox
}
}The class defines two overloaded constructors and one ordinary method, making every difference visible: the constructors share the class name and declare no return type, while getBrowser() has its own name and returns a String. The no-arg constructor demonstrates constructor chaining by calling this("chrome") as its first statement, so creating d1 prints the parameterized constructor's message before the no-arg constructor's own message.
Note the terminology: a no-arg constructor you write yourself is not a "default constructor" — that term refers only to the one the compiler generates when you write none. Each constructor runs exactly once per new call, whereas getBrowser() can be invoked repeatedly on the same object.
Go beyond the definition: mention that constructors are not inherited, cannot be overridden or static, and that super() or this() must be the first statement in a constructor body. Expect follow-ups like "What happens if you add a return type to a constructor?" (it silently becomes an ordinary method, which is a classic trap) and "Can a constructor be private?" (yes, as used in the Singleton pattern for a shared WebDriver instance).
Tie it to your framework by explaining how your Page Object constructors accept WebDriver and call PageFactory.initElements.