use*_*932 43 java constructor return-type
可能重复:
为什么构造函数不返回值
为什么构造函数不具有返回类型,甚至无效?这是什么原因?
zac*_*usz 49
构造函数在内部是具有名称<init>
和void
返回类型的非静态方法.它没有返回任何东西.在内部分配第一个对象,然后调用其构造函数.对象未使用构造函数本身分配.
换句话说,语法new Object()
不仅调用构造函数,还创建新对象,并在调用构造函数后返回它.在太阳的Java教程代表说:'新的运营商之后的构造函数,它初始化新对象的调用.’ 初始化并不意味着创建.
回答这个问题.缺少返回类型声明是一种将构造函数与方法区分开的方法.但是你可以从构造函数返回,从void方法返回.例如,此代码编译并正确运行:
public class TheClass {
public TheClass(){
return;
}
public void TheClass(){ //confusing, but this is void method not constructor
return;
}
public static void main(String[]a){
TheClass n = new TheClass();
n.TheClass();//void method invocation
}
}
Run Code Online (Sandbox Code Playgroud)
这个类有一个void方法(不要在家里尝试 - 大写方法是一个坏样式)和一个构造函数.区别在于声明的返回类型.
看看这个JNI代码片段,它演示了构造函数是一个非静态void方法:
jstring
MyNewString(JNIEnv *env, jchar *chars, jint len)
{
jclass stringClass;
jmethodID cid;
jcharArray elemArr;
jstring result;
stringClass = (*env)->FindClass(env, "java/lang/String");
if (stringClass == NULL) {
return NULL; /* exception thrown */
}
/* Get the method ID for the String(char[]) constructor */
cid = (*env)->GetMethodID(env, stringClass,
"<init>", "([C)V");
if (cid == NULL) {
return NULL; /* exception thrown */
}
/* Create a char[] that holds the string characters */
elemArr = (*env)->NewCharArray(env, len);
if (elemArr == NULL) {
return NULL; /* exception thrown */
}
(*env)->SetCharArrayRegion(env, elemArr, 0, len, chars);
result = (*env)->AllocObject(env, stringClass);
if (result) {
(*env)->CallNonvirtualVoidMethod(env, result, stringClass,
cid, elemArr);
/* we need to check for possible exceptions */
if ((*env)->ExceptionCheck(env)) {
(*env)->DeleteLocalRef(env, result);
result = NULL;
}
}
/* Free local references */
(*env)->DeleteLocalRef(env, elemArr);
(*env)->DeleteLocalRef(env, stringClass);
return result;
}
Run Code Online (Sandbox Code Playgroud)
特别是这些片段:
/* Get the method ID for the String(char[]) constructor */
cid = (*env)->GetMethodID(env, stringClass, "<init>", "([C)V");
Run Code Online (Sandbox Code Playgroud)
然后
/* Allocate new object. */
result = (*env)->AllocObject(env, stringClass);
if (result) {
/* Call uninitialized objects' constuctor. */
(*env)->CallNonvirtualVoidMethod(env, result, stringClass, cid, elemArr);
Run Code Online (Sandbox Code Playgroud)
分配第一个对象,然后<init>
调用非静态方法.详情请看这里.该AllocObject函数文档代表的是"分配一个新的Java对象而不调用任何的构造函数的对象.返回的对象的引用." 所以在JVM对象中没有由构造函数分配,而只是由它初始化.查看构造函数的字节码,我们发现没有返回任何对象(与void方法完全相同).
另一种方法是,当您对样本类进行分解时,您将看到从其构造函数调用parent(Object)构造函数:
#javap -c NewClass
Compiled from "NewClass.java"
public class NewClass extends java.lang.Object{
public NewClass();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
}
Run Code Online (Sandbox Code Playgroud)
请注意,该<init>
方法实际上不是Java语言的一部分.相反,它是Java虚拟机期望在Java类文件中看到的东西.这种区别很重要,因为Java语言不依赖于类文件.Java源代码可以编译成其他二进制格式,包括本机可执行文件.将Java语言源转换为其他二进制格式的Java编译器无需生成命名的方法<init>
,只要在适当的时间以适当的方式初始化对象即可.Java语言规范(JLS)详细说明了初始化的顺序及其发生的时间,但没有说明它是如何实现的.
但是我看到我们在这里谈论JVM.
对于一些非信徒来说,这是示例(thx biziclop),它显示对象存在并在从构造函数返回之前分配:
class AnotherClass {
private String field;
public static AnotherClass ref;
public AnotherClass() {
this.field = "value";
AnotherClass.ref = this;
throw new RuntimeException();
}
@Override
public String toString() {
return field;
}
}
public class MainClass {
public static void main(String[] a) {
try {
new AnotherClass();
return;
} catch (RuntimeException ex) {
System.out.println("exception");
}
System.out.println("instance: " + AnotherClass.ref);
}
}
Run Code Online (Sandbox Code Playgroud)