Tricky Core Java Interview Questions Interviewer’s Favourite Topics(Constructors Latest)

Hello All, In continuation to the series of my core java interview questions, today I’m sharing Constructors related questions that can help you ace your technical interviews.
I’ve made it easy for you to refer interview questions as per the topics(Core Java, Microservices, Spring Boot, Design Patterns, AWS, etc.) and prepared lists below.
What are constructors in Java, and what are their key features in modern Java?
Constructors are special methods used to initialize objects. In the latest Java versions:
- Constructors support records introduced in Java 14.
- Enhanced by sealed classes introduced in Java 17.
Example:
Record Constructor (Java 14+):
public record Employee(String name, int id) {
// Compact constructor
public Employee {
if (id < 0) {
throw new IllegalArgumentException("ID cannot be negative");
}
}
}How do records handle constructors?
Records automatically generate:
- A canonical constructor with parameters for all fields.
- The ability to define custom constructors.
Example:
public record Product(String name, double price) {
public Product {
if (price < 0) {
throw new IllegalArgumentException("Price cannot be negative");
}
}
// Custom constructor
public Product(String name) {
this(name, 0.0); // Default price
}
}What is the difference between constructors and static factory methods in modern Java?

Example (Static Factory Method):
class Employee {
private final String name;
private Employee(String name) {
this.name = name;
}
// Static factory method
public static Employee createEmployee(String name) {
return new Employee(name);
}
}How do sealed classes interact with constructors? (Java 17+)
Sealed classes restrict which classes can extend them. Subclasses of sealed classes often define specific constructors to match their role.
Example:
sealed class Shape permits Circle, Rectangle {}
final class Circle extends Shape {
private final double radius;
Circle(double radius) {
this.radius = radius;
}
}
final class Rectangle extends Shape {
private final double length, width;
Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
}Can you use constructors with var in modern Java?
You cannot use var in constructor declarations because the type must be explicit. However, var can be used inside constructors for local variable declarations.
Example:
class Example {
private int value;
Example(int value) {
var temp = value; // `var` used for local variable
this.value = temp;
}
}
How does Java handle constructors in enums?
- Enum constructors are private and can’t be invoked explicitly outside the enum.
- Used for initializing constant-specific fields.
Example:
enum Day {
MONDAY("Start of the week"),
FRIDAY("End of the week");
private final String description;
private Day(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}How has constructor behavior changed with text blocks (Java 15+)?
Text blocks allow multi-line strings to be used in constructors, making code cleaner and more readable.
Example:
class Query {
private final String sql;
Query(String sql) {
this.sql = sql;
}
public static void main(String[] args) {
Query query = new Query("""
SELECT *
FROM users
WHERE age > 18
""");
System.out.println(query.sql);
}
}Can constructors work with Optional in modern Java?
Yes, constructors can use Optional to handle nullable or optional parameters.
Example:
class User {
private final String name;
private final String email;
User(String name, Optional<String> email) {
this.name = name;
this.email = email.orElse("No email provided");
}
}
What are compact constructors in records? How do they work?
Compact constructors are used in records to validate or modify input parameters without redeclaring fields.
Example:
public record Rectangle(int length, int width) {
public Rectangle {
if (length <= 0 || width <= 0) {
throw new IllegalArgumentException("Dimensions must be positive");
}
}
}Can constructors be used in pattern matching? (Java 20 Preview)
Yes, with pattern matching for records, constructors can indirectly enable destructuring and pattern matching.
Example:
record Point(int x, int y) {}
class Main {
static void printPoint(Object obj) {
if (obj instanceof Point(int x, int y)) {
System.out.println("Point: " + x + ", " + y);
}
}
public static void main(String[] args) {
printPoint(new Point(10, 20));
}
}How can constructors be used in concurrency-safe initialization?
Answer:
Use constructors with immutability and lazy initialization techniques (e.g., using record or synchronized blocks).
Example (Lazy Initialization):
class Singleton {
private static Singleton instance;
private Singleton() {
// Constructor
}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
How are constructors used in switch expressions (Java 14+)?
Constructors can be invoked within enhanced switch expressions for object creation.
Example:
class Shape {
String type;
Shape(String type) {
this.type = type;
}
public static Shape fromType(String type) {
return switch (type) {
case "circle" -> new Shape("Circle");
case "square" -> new Shape("Square");
default -> throw new IllegalArgumentException("Unknown shape");
};
}
}What is the relationship between constructors and sealed hierarchy finalization?
Constructors enforce initialization rules when extending a sealed class. Subclasses can invoke specific superclass constructors.
Example:
sealed class Animal permits Dog {}
final class Dog extends Animal {
Dog(String name) {
super(); // Explicit or implicit call to the sealed superclass constructor
}
}What are key considerations when working with constructors in streams?
Constructors can be used as method references in stream operations to create objects.
Example:
import java.util.stream.Stream;
class Person {
String name;
Person(String name) {
this.name = name;
}
}
class Main {
public static void main(String[] args) {
Stream.of("Alice", "Bob")
.map(Person::new) // Constructor reference
.forEach(p -> System.out.println(p.name));
}
}How does immutability in constructors play a role in modern Java design?
Constructors in records and final classes help enforce immutability by ensuring fields are initialized only once, providing thread safety and simplicity.
Example (Immutable Class):
final class ImmutablePerson {
private final String name;
ImmutablePerson(String name) {
this.name = name;
}
public String getName() {
return name;
}
}Hope you all liked the content. Clap if u like this n follow me for more interesting content .It will be great if you show your love and encouragement :)
Meet you all with my next article soon.
Meanwhile, keep reading my other articles too :) https://medium.com/me/lists
Thank You.
Stay healthy and happy.
Stackademic 🎓
Thank you for reading until the end. Before you go:
- Please consider clapping and following the writer! 👏
- Follow us X | LinkedIn | YouTube | Discord | Newsletter | Podcast
- Create a free AI-powered blog on Differ.
- More content at Stackademic.com





