Home > Java > javaTutorial > Java Constructors: What\'s the Difference Between \'void\' and \'non-void\' Constructors?

Java Constructors: What\'s the Difference Between \'void\' and \'non-void\' Constructors?

Barbara Streisand
Release: 2024-11-27 12:06:14
Original
570 people have browsed it

Java Constructors: What's the Difference Between

Understanding Java Constructors: "void" vs. "non-void"

In Java, a constructor is a special method that initializes an instance of a class. Unlike regular methods, constructors do not specify a return type; instead, they have the same name as the class itself. However, Java allows for confusion by allowing so-called "void" constructors.

Example 1: Non-Void Constructor

In the following code, the constructor class1() is declared without specifying a return type:

public class class1 {
    public static Integer value = 0;
    public class1() {
        da();
    }
    public int da() {
        class1.value = class1.value + 1;
        return 5;
    }
    public static void main(String[] args) {
        class1 h = new class1();
        class1 h2 = new class1();
        System.out.println(class1.value); // Output: 2
    }
}
Copy after login

Explanation:

  • The class1() constructor initializes the value static field to 0.
  • When instances h and h2 of class1 are created, the constructor is called.
  • The da() method increments value, resulting in a final value of 2.

Example 2: Void Constructor

Now consider this code with a "void" constructor:

public class class1 {
    public static Integer value = 0;
    public void class1() {
        da();
    }
    public int da() {
        class1.value = class1.value + 1;
        return 5;
    }
    public static void main(String[] args) {
        class1 h = new class1();
        class1 h2 = new class1();
        System.out.println(class1.value); // Output: 0
    }
}
Copy after login

Explanation:

  • The class1() "void" constructor does not initialize the value field or call any methods.
  • When instances h and h2 are created, the default constructor is called, which does not perform any initialization.
  • Consequently, the value field remains at its initial value of 0.

Conclusion:
In Java, constructors that declare a return type, even if it's void, are not constructors at all. They are simply methods. True constructors, which initialize class instances, never specify a return type. If no constructor is explicitly defined, Java automatically adds a default constructor that performs no initialization. Understanding this distinction is crucial for correct class initialization in Java.

The above is the detailed content of Java Constructors: What\'s the Difference Between \'void\' and \'non-void\' Constructors?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template