Skip to content
Learn Netverks

Lesson

Step 13/36 36% through track

constructors

Constructors

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Constructors: the syntax, APIs, and habits you need before advancing in Java.

Teams ship Constructors on every Java codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Constructors in contexts like: Spring Boot APIs, banking systems, Android (with Kotlin), and batch/data pipelines on the JVM.

Write Java with a public class (lessons use Main), click Run on server—the dev runner runs javac then java; fix compile errors from stderr (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

A constructor initializes new objects. It shares the class name, has no return type, and runs when new is called. If you omit one, Java provides a default no-arg constructor until you define any constructor.

Example

class Product {
    final String sku;
    final double price;

    Product(String sku, double price) {
        this.sku = sku;
        this.price = price;
    }
}

this refers to the current instance—disambiguates parameters from fields.

Constructor chaining

Product(String sku) {
    this(sku, 0.0);
}

Important interview questions and answers

  1. Q: Constructor vs method?
    A: Same name as class, no return type, invoked only via new.
  2. Q: What happens when you add a parameterized constructor?
    A: Default no-arg constructor is not generated—you must define it explicitly if needed.

Self-check

  1. What keyword calls another constructor in the same class?
  2. Why use final fields set in the constructor?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Default constructor gone?
  • this() chain?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump