DEV Community

Oleksandr
Oleksandr

Posted on

Java: Constructors, Return Methods, Comments

Hey everyone!

In my previous post, I covered Objects, Methods, and Output—key concepts in Java. Today, we’ll continue exploring Object-Oriented Programming (OOP) by diving into сonstructors, methods with return values, comments

Comments in Java

Comments are a way to write notes for yourself or provide explanations for other programmers within your code. They are ignored by the compiler when the code runs, meaning they don’t affect the program’s execution—they are only for humans to read.

What is compiler?

A compiler is a program that translates your Java code into a format that the computer can understand and execute.

Java supports different types of comments:

  • Single-line comment (starts from //), used for short notes or explanations. Example:
// Next line print car's brand name
System.out.println("Brand name: " + brandName); // Remember about ';' 
Enter fullscreen mode Exit fullscreen mode
  • Multi-line comments(starts from /* and ends with */), used when you need to write longer explanations.
/*
The following line prints the car's model.
Multi-line comments are useful for detailed explanations.
*/
System.out.println("Model: " + model);
Enter fullscreen mode Exit fullscreen mode

Constructors in Java

In simple terms, a constructor in Java is a special method used to create (initialize) objects. It runs automatically when an object of a class is created.

Here are some key points about constructors:

  • The constructor has the same name as the class
  • It does not have a return type
  • It is used to set initial values for object properties(variables)

Example of a constructor:

class Car {
    String brandName;
    String model;
    int maxSpeed;
    double length;
    boolean isEngineWorks;

    // Constructor
    Car() { 
        // Initialize all variables in the constructor
        brandName = "BMW";
        model = "M5";
        maxSpeed = 250;
        length = 4.983;
        isEngineWorks = true;
    }

    void outputValues() {
        System.out.println("Brand name: " + brandName);
        System.out.println("Model: " + model);
        System.out.println("Max Speed: " + maxSpeed);
        System.out.println("Length: " + length);
        System.out.println("Engine Works: " + isEngineWorks);
    }
    public static void main(String[] args) {
        Car car = new Car(); // Calling the constructor when creating an object
        car.outputValues();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Brand name: BMW
Model: M5
Max Speed: 250
Length: 4.983
Engine Works: true
Enter fullscreen mode Exit fullscreen mode

If you do not write any constructor for a class, Java will automatically use a default constructor.
However, the default constructor does not initialize variables - it only creates an object.

Constructors in Java can also accept parameters to initialize variables dynamically.

Example of a constructor with parameters:

class Car {
    String brandName;
    String model;
    int maxSpeed;
    double length;
    boolean isEngineWorks;

    // Constructor with parameters
    Car(String brandName, String model, int maxSpeed, double length, boolean isEngineWorks) {
        this.brandName = brandName;
        this.model = model;
        this.maxSpeed = maxSpeed;
        this.length = length;
        this.isEngineWorks = isEngineWorks;
    }

    void outputValues() {
        System.out.println("Brand name: " + brandName);
        System.out.println("Model: " + model);
        System.out.println("Max Speed: " + maxSpeed);
        System.out.println("Length: " + length);
        System.out.println("Engine Works: " + isEngineWorks);
    }

    public static void main(String[] args) {
        // Passing values to the constructor
        Car car = new Car("BMW","M5",250,4.983,false); 
        car.outputValues();

    }
}

Enter fullscreen mode Exit fullscreen mode

Output:

Brand name: BMW
Model: M5
Max Speed: 250
Length: 4.983
Engine Works: false
Enter fullscreen mode Exit fullscreen mode

Keyword this
In the previous example, you might have noticed the use of this.
The this keyword refers to the current object’s variables.
It helps differentiate between class variables and constructor parameters when they have the same name.

Methods with parameters

Like constructors, methods can also receive parameters that we can use inside them.
Example of a method with parameters:

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.printMessage("Hello");
    }

    // Method with parameters message
    void printMessage(String message) {
        System.out.println(message);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Hello
Enter fullscreen mode Exit fullscreen mode

Method with return type

In Java, a method with a return type is a method that returns a value after execution. The return type specifies the type of data the method will return.
Example of a method with return type:

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        int a= main.returnInt();
        String b= main.returnString();
        boolean c= main.returnBoolean();
        double d= main.returnDouble();

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }

    int returnInt() {
        return 1;
    }

    String returnString() {
        return "Hello";
    }

    boolean returnBoolean() {
        return true;
    }

    double returnDouble() {
        return 1.0;
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

1
Hello
true
1.0
Enter fullscreen mode Exit fullscreen mode

You can also use return value for simple output:

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        System.out.println(main.returnString());
    }

    String returnString() {
        return "Hello";
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Hello
Enter fullscreen mode Exit fullscreen mode

Note that a method can also have a void return type if it does not need to return a value.

Keyword return

The return keyword specifies which value Java should return as the result of the method execution.

Summary

✅ Constructors help us to initialize objects.
✅ Methods can return values using the return keyword.
✅ Comments make code more understandable.

What's next?

In the next post, we'll discover the topic of encapsulation, Getters and Setters(practical use of encapsulation), access modifiers and why we should not change values directly.

In order to better learn the material, I advise you to write your own class that would contain methods with return types, default and parameterized constructors (try to use them both to create two different objects) and also practice with comments.

Top comments (0)