java中的默认构造函数的实际用途是什么? [英] What is the actual use of the default constructor in java?

查看:96
本文介绍了java中的默认构造函数的实际用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了许多资料和书籍,其中介绍了构造函数用于在创建对象时初始化字段的方法。我还读到,如果我不提JVM,那么JVM提供了一个默认的构造函数。如果不需要像下面提到的那样初始化字段,构造函数的目的是什么?

I have read in many sources and books that constructor is used to initialise the fields when an object is created. I have also read that JVM provides a default constructor if I don't mention one. What is the purpose of a constructor if it is not required to initialise the fields like the one I have mentioned below?


我也理解没有参数的构造函数是必需的,因为
在对象创建过程中没有传递参数
时是必须的(当存在带有参数的程序员定义的构造函数时)。

I also understand that a constructor without parameter is required as it is necessary during object creation when an argument is not passed (when programmer-defined constructors with parameters exists).

JVM是否需要提供实际上不需要的构造函数?

Is it necessary for JVM to provide a constructor which is actually not required?

 public class test {
        int a;
        public test() { 
       //this constructor is useless

    }
public static void main(String[] args)
{ 
  test ob= new test();
  System.out.println(ob.a); 
//this prints 0 which means a constructor is not required in intialising `a`
}
}

$时不需要构造函数b
$ b

如果程序员定义了类似 test()的构造函数,则可能有意义,因为可能会有其他构造函数接受参数。
但是当程序员没有声明构造函数时,为什么JVM有必要提供一个?

A constructor like test() makes sense if the programmer defines it since there could be other constructors which takes argument. But why is it necessary for JVM to provide one when no constructor is declared by the programmer?

我已经测试并证明初始化字段不会

I have already tested and proved that initialising a field doesn't require constructor.

默认构造函数又是什么样子?

Also what does the default constructor look like?

推荐答案

问题是,尽管您知道默认的构造函数在此示例中不执行任何操作,但将来即使您没有意识到默认构造函数也可能会执行某些操作,并且可能无法在所有位置重新编译代码使用可靠。因此,最安全,最简单的方法是始终调用一个将来可能会更改的构造函数,如果JIT实际上没有执行任何操作,则让JIT优化该构造函数。

The problem is that while you know the default constructor doesn't do anything in this example, in future the constructor might do something even if you don't realise it is and you might not be able to re-compile everywhere the code is used reliably. So the safest, simplest thing to do is to always call a constructor which might change in the future and let the JIT optimise away the constructor if it doesn't actually do anything.

无论您提供与否,字节码始终调用构造函数。当您编译使用默认构造函数的代码时,不能假设该构造函数没有做任何有用的事情,因为您可以稍后在其中添加一些内容以做一些有用的事情。例如

The byte code always calls a contructor, whether you provide one or not. When you compile code which uses the default constructor it cannot assume the constructor doesn't do anything useful as you can add something to it later to do something useful. e.g.

您可以更改

public class Test {
    int a;

    // public Test() { //this constructor does nothing
}

public class Test {
    int a;
    final List<String> strings = new ArrayList<>();

    // public Test() { //this constructor does something now.
}

public class ServerTest {
    final List<String> strings = new ArrayList<>();
}

public class Test extends SuperTest {
   int a;
   // the default constructor has to call super();
}

构造函数现在初始化了字符串字段。您可以更改此类,而无需在任何使用它的地方都重新编译,然后说,嘿,我的构造函数现在做了一些有用的事情,您应该立即调用它。

The constructor now initialised the strings field. You can change this class without having to re-compile everywhere it is used and say, hey, my constructor now does something useful, you should call it now.

这篇关于java中的默认构造函数的实际用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆