简体   繁体   English

创建对象后,是否可以看到默认的构造函数调用? 在java中

[英]Can I see default constructor call Once object is creating | in java

Just a public class A that will have the default constructor defined by JVM and going to call at run time 只是一个公共类A,它将具有JVM定义的默认构造函数,并在运行时进行调用

public class A {

}

Main class has main method which will create the object of class A and automatically call the default constructor of the class A. Main类具有main方法,该方法将创建A类的对象并自动调用A类的默认构造函数。

public class Main {
    public static void main(String[] args) {
        A a = new A();
    }
}

Is it possible for me to do the debug and can see the cursor flow that end-up into calling default constructor of class A? 我可以进行调试,并且可以看到游标流最终进入调用类A的默认构造函数吗?

There will be no use of debug unless you have some business logic inside. 除非您具有一些业务逻辑,否则将不会使用调试。 Otherwise it is empty. 否则为空。

But Yes you can. 但是可以。

public class A {

    /* Default no arg constructor */
    public A(){
       System.out.println("Put a break point at this line");
     }
    }

You can still able to run this code without default constructor where JVM internally inserts it. 您仍然可以在没有JVM内部插入默认构造函数的情况下运行此代码。

If you want to add some functionality to, you need to write it manually. 如果要添加一些功能,则需要手动编写。

There is no direct way. 没有直接的方法。

You can define the default constructor and print something into it. 您可以定义默认构造函数并在其中打印一些内容。

class A {
  public A() {
    System.out.println("constructor is called");
  }
}

Now when you create an instance of A class in Main class, the message will be printed. 现在,当您在Main class中创建A类的实例时,将打印该消息。 You can put the debug point into the print statement and see the runtime cursor coming at it. 您可以将调试点放入print语句中,然后查看运行时光标。

Why would you even want to do this? 你为什么还要这么做? As the jls states: jls所述:

If the class being declared is the primordial class Object, then the default constructor has an empty body. 如果要声明的类是原始类Object,则默认构造函数的主体为空。 Otherwise, the default constructor simply invokes the superclass constructor with no arguments. 否则,默认构造函数将简单地调用不带参数的超类构造函数。

So there's nothing interesting to see. 因此,没有什么有趣的看到的。 Just some calls of the default-constructors up to Object . 只需调用默认构造函数,直到Object As for the debugging-question, this is highly dependant upon the debugger you use. 至于调试问题,这在很大程度上取决于您使用的调试器。

Since the default constructor is empty by definition, putting code inside it like some answers suggested makes no sense: it won't be a default constructor anymore. 由于默认构造函数在定义上为空,因此像建议的一些答案一样将代码放入其中是没有意义的:它将不再是默认构造函数。

I don't have an IDE with me now (on the phone), but I think you should be able to step into the default constructor from a statement that creates a new instance of the class, for example, from a line like this: 我现在没有(电话上)有一个IDE,但是我认为您应该能够从创建该类的新实例的语句进入默认构造函数,例如,从这样的一行开始:

Something something = new Something();

However, what's the point? 但是,有什么意义呢? The default constructor is empty, so there's nothing to see or debug. 默认构造函数为空,因此没有任何内容可查看或调试。

If you want to stop execution when an instance of the class is created by the default constructor, then create a parameterless constructor with a dummy statement in it. 如果要在默认构造函数创建类的实例时停止执行,请创建一个无参数构造函数,并在其中创建一个伪语句。 (Of course, it won't be, there won't be a default constructor anymore, as noted earlier.) (当然,不会,不再有默认的构造函数,如前所述)。

If you cannot edit the source code of the class, then I don't know how to stop execution in the default constructor. 如果您无法编辑该类的源代码,那么我不知道如何在默认构造函数中停止执行。 I'd be very interested to learn. 我会很感兴趣学习。

暂无
暂无

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

相关问题 在 Java 中创建 object 时未调用默认构造函数 - Default Constructor not called while creating object in Java 我可以在java中的公共类中调用参数化构造函数中的默认构造函数吗? - Can I call default constructor from parameterized constructor inside public class in java? 创建没有默认构造函数的对象 - Creating an object without a default constructor 我可以在Java中用构造函数调用方法吗? - Can I call methods in constructor in Java? 我们可以通过创建一个对象来调用类的所有构造函数吗 - Can we Call all constructor of a class by creating one object 我们可以在Java中调用对象构造函数内的对象的synchronized方法吗? - Can we call synchronized method of an object inside the constructor of the object in Java? 调用构造函数后,是否可以使对象不变? - Can I make object unmutable once the constructor is called? 什么体系结构/ OS其他线程可以在构造函数调用后看到默认的非最终字段值? - in what architectures/OS other thread can see default nonfinal field values after constructor call? 如何在Java的构造函数中设置默认的String格式? - How can I set a default String format in my constructor in Java? 如何从另一个类中的public类型的参数化构造函数中调用默认类型的参数化构造函数? - How can I call parameterized constructor of default type from a parameterized constructor of type public which is in another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM