可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
As we know that we do not have to add any return type to a Java constructor.
class Sample{
.....
Sample(){
........
}
}
In Objective C, if we create a constructor, it returns a pointer to its class. But it is not compulsory, I think.
AClass *anObject = [[AClass alloc] init];//init is the constructor with return type a pointer to AClass
Similarly, Is the constructor converted to a method which return a reference to its own class??
Like this:
class Sample{
.....
Sample Sample(){
........
return this;
}
}
Does the compiler add a return type a reference to same class to constructor?
What is happening to a constructor?
Any reference to study this?
EDIT:
Actually i want the answers to be at byte code level or JVM level or even below.
回答1:
Many have answered how constructors are defined in Java.
At the JVM level, static initialisers and constructors are methods which return void. Static initialisers are static methods, however constructors use this
and don't need to return anything. This is because the caller is responsible for creating the object (not the constructor)
If you try to only create an object in byte code without calling a constructor you get a VerifyError. However on the oracle JVM you can use Unsafe.allocateInstance() to create an object without calling a constructor,
The static initialiser is called <cinit>
which takes no arguments and the constructor is called <init>
. Both have a void return type.
For the most part, this is hidden from the Java developer (unless they are generating byte code) however the only time you see these "methods" in stack traces (though you can't see a return type)
回答2:
While constructors are similar to methods, they are not methods. They have no return type, are not inherited, and cannot be hidden or overridden by subclasses.
Constructors are invoked by class instance-creation expressions (basically, the use of new
), by explicit invocation from other constructors (using this(...)
or super(...)
syntax), and by the string concatenation operator. There is no other way to invoke a constructor (in particular, they cannot be invoked like other methods).
See Section 8.8 of the Java Language Specification for more info.
回答3:
Is the constructor converted to a method which return a reference to its own class??
No but yes, if it is specified to do so.
Does compiler add a return type a reference to same class to constructor ??
No it does not
What is happening to a constructor??
It is the method, which runs when the object is created. Typically, by using "new" keyword. It Might perform some preliminary task, or return something or assign some values during construction.
Any reference to study this.??
- http://www.javaworld.com/javaworld/jw-10-2000/jw-1013-constructors.html
- http://www.javabeginner.com/learn-java/java-constructors
回答4:
Constructors are similar to methods except that they use the name of the class and have no return type. The whole purpose of using constructors is to create an object (an instance of a class) and allocate it (via new
keyword) in the memory (the heap) and also initialize any fields if available.
回答5:
Constructors are invoked via the special java keyword new
, which creates (and initializes) an object of the specified concrete type.
I suppose you could say the combination of new
and the chosen constructor "returns" an object, which in java is of course a pointer under the covers
回答6:
Constructor returns the class reference of the class for which its being called.E.g.-
class A {
int x;
A(int a) {
x = a;
}
}
class B {
public static void main(String asd[]) {
A a = new A(4);
System.out.println(a);
}
}
Here after calling the constructor A(...)
, this constructor will return the reference of type of class A
to caller( i.e. A a = new A(4)
).
回答7:
The return type of the constructor is corresponding class type.
package com.ie.test;
import java.lang.reflect.*;
public class a {
public a() {
super();
System.out.println("*** no-arg constructor ***");
}
public static void main(String[] args) {
Constructor[] constructors = a.class.getConstructors();
for (Constructor constructor:constructors) {
int i = constructor.getModifiers();
AnnotatedType annotatedType = constructor.getAnnotatedReturnType();
System.out.println("***********Returntype *******"+annotatedType.getType());
System.out.println("*******constructor *****"+Modifier.toString(i));
}
Method[] methods = a.class.getDeclaredMethods();
for (Method method:methods) {
int i = method.getModifiers();
// Class c = method.getReturnType();
AnnotatedType annotatedType = method.getAnnotatedReturnType();
System.out.println("***********Returntype *******"+annotatedType.getType());
// System.out.println(c);
System.out.println("*******methods*******"+Modifier.toString(i));
}
}
public int m1() {
System.out.println("***************");
return 0;
}
}
回答8:
Constructor is only used to only initialize class member and
- Constructor Name and class name are same
- Constructor can not have return type
- Constructor always called when object is created
- Constructor always public