java“void”和“non void”构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24963718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java "void" and "non void" constructor
提问by Hadi
I wrote this simple class in java just for testing some of its features.
我用java编写了这个简单的类只是为了测试它的一些功能。
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);
}
}
The output is:
输出是:
2
2
But in this code:
但在这段代码中:
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);
}
}
The output of this code is:
这段代码的输出是:
0
0
So why doesn't, when I use void
in the constructor method declaration, the static field of the class doesn't change any more?
那么为什么不,当我void
在构造函数方法声明中使用时,类的静态字段不再改变?
采纳答案by Luiggi Mendoza
In Java, the constructor is nota method. It only has the name of the class and a specific visibility. If it declares that returns something, then it is not a constructor, not even if it declares that returns a void
. Note the difference here:
在 Java 中,构造函数不是方法。它只有类的名称和特定的可见性。如果它声明返回一些东西,那么它不是一个构造函数,即使它声明返回一个void
。注意这里的区别:
public class SomeClass {
public SomeClass() {
//constructor
}
public void SomeClass() {
//a method, NOT a constructor
}
}
Also, if a class doesn't define a constructor, then the compiler will automatically add a default constructor for you.
此外,如果一个类没有定义构造函数,那么编译器会自动为你添加一个默认构造函数。
回答by Khary Mendez
public void class1() is not a constructor, it is a void method whose name happens to match the class name. It is never called. Instead java creates a default constructor (since you have not created one), which does nothing.
public void class1() 不是构造函数,它是一个void 方法,其名称恰好与类名匹配。它永远不会被调用。相反,java 创建了一个默认构造函数(因为您还没有创建),它什么都不做。
回答by Cal Stephens
Using void in the constructor by definition leads it to not longer be the constructor. The constructor specifically has no return type. While void doesn't return a value in the strictest sense of the word, it is still considered a return type.
根据定义在构造函数中使用 void 会导致它不再是构造函数。构造函数特别没有返回类型。虽然 void 不返回最严格意义上的值,但它仍被视为返回类型。
In the second example (where you use the void), you would have to do h.class1()
for the method to get called because it is no longer the constructor. Or you could just remove the void.
在第二个示例中(使用 void 的地方),您必须要h.class1()
调用该方法,因为它不再是构造函数。或者你可以删除空白。
回答by Lin Sherman
This is arguably a design flaw in Java.
这可以说是 Java 中的一个设计缺陷。
class MyClass {
// this is a constructor
MyClass() {...}
// this is an instance method
void MyClass() {...}
}
Perfectly legal. Probably shouldn't be, but is.
完全合法。可能不应该,但确实如此。
In your example, class1() is never getting called, because it's not a constructor. Instead, the default constructor is getting called.
在您的示例中, class1() 永远不会被调用,因为它不是构造函数。相反,默认构造函数被调用。
Suggestion: familiarize yourself with Java naming conventions. Class names should start with uppercase.
建议:熟悉 Java 命名约定。类名应以大写开头。