简体   繁体   English

java中构造函数的返回类型是什么?

[英]What is the return type of a constructor in java?

As we know that we do not have to add any return type to a Java constructor.正如我们所知,我们不必向 Java 构造函数添加任何返回类型。

class Sample{
  .....
  Sample(){
    ........
  }
}

In Objective C, if we create a constructor, it returns a pointer to its class.在 Objective C 中,如果我们创建一个构造函数,它会返回一个指向它的类的指针。 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.实际上,我希望答案在字节码级别或 JVM 级别甚至更低。

Many have answered how constructors are defined in Java.许多人已经回答了如何在 Java 中定义构造函数。

At the JVM level, static initialisers and constructors are methods which return void.在 JVM 级别,静态初始化器和构造器是返回 void 的方法。 Static initialisers are static methods, however constructors use this and don't need to return anything.静态初始化器是静态方法,但是构造函数使用this并且不需要返回任何东西。 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,但是在 oracle JVM 上,您可以使用 Unsafe.allocateInstance() 创建一个对象,而无需调用构造函数,

The static initialiser is called <cinit> which takes no arguments and the constructor is called <init> .静态初始化器称为<cinit> ,它不带参数,构造器称为<init> Both have a void return type.两者都有一个 void 返回类型。

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)在大多数情况下,这对 Java 开发人员是隐藏的(除非他们生成字节码)但是唯一一次您在堆栈跟踪中看到这些“方法”(尽管您看不到返回类型)

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.构造函数由类实例创建表达式(基本上是使用new )、其他构造函数的显式调用(使用this(...)super(...)语法)以及字符串连接运算符调用。 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.有关更多信息,请参阅Java 语言规范的第 8.8 节

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.通常,通过使用“new”关键字。 It Might perform some preliminary task, or return something or assign some values during construction.它可能会执行一些初步任务,或者在构造过程中返回一些东西或分配一些值。

Any reference to study this.??任何参考研究这个。?

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.使用构造函数的全部目的是创建一个对象(类的实例)并在内存(堆)中分配它(通过new关键字),并在可用时初始化任何字段。

Constructors are invoked via the special java keyword new , which creates (and initializes) an object of the specified concrete type.构造函数通过特殊的 java 关键字new调用,它创建(并初始化)指定具体类型的对象。

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我想你可以说new和所选构造函数的组合“返回”一个对象,它在 java 中当然是封面下的指针

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;
    }
}

Constructor returns the class reference of the class for which its being called.Eg-构造函数返回调用它的类的类引用。例如-

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( ie A a = new A(4) ).这里在调用构造函数A(...) ,这个构造函数会将类A的类型的引用返回给调用者(即A a = new A(4) )。

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 构造函数总是公开的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM