简体   繁体   English

在 Java 中,为什么未将类的 New 实例分配给变量

[英]In Java why is the New instance of a class not assigned to a variable

Total beginner in Java. Java 初学者。 I have seen the line below several times and cannot understand it ... new TestCode() - it SEEMS to be an instatiation but not assigned to anything - So how can it be referred to ?我已经多次看到下面的行并且无法理解它...... new TestCode() - 它似乎是一个实例,但没有分配给任何东西 - 那么如何引用它呢?

class TestCode
{
   public TestCode()
   {
       System.out.println();
       System.out.println( "Welcome to the Program");
    }

    public static void main( String args[] ){new TestCode();}

}

It cannot be referred to.它不能被引用。 The new instance is created and immediately discarded.新实例被创建并立即丢弃。

However its constructor code gets invoked, therefore side-effects (if there are such - and in your example there are - it's printing to stdout) do occur.然而,它的构造函数代码被调用,因此会发生副作用(如果有的话——在你的例子中有——它会打印到标准输出)。 Doing stuff like this is however a bad practice.然而,做这样的事情是一种不好的做法。

See Zaki's answer about assigning it to variable though.请参阅 Zaki 关于将其分配给变量的答案。 Then you can refer to it, and do stuff.然后你可以参考它,做一些事情。

When you instantiate an object via the new codeword, space on the heap (the part of the system memory used to store objects and their fields) is allocated, and the constructor gets executed.当你通过new代码字实例化一个对象时,堆上的空间(用于存储对象及其字段的系统内存部分)被分配,并且构造函数被执行。 In this case, the constructor writes on the standard output, and this gets executed regardless whether a reference is created to the object or not.在这种情况下,构造函数写入标准输出,无论是否创建对对象的引用,都会执行此操作。

Afterwards, you're right, the object can not be accessed as no references to it exist.之后,您是对的,由于不存在对它的引用,因此无法访问该对象。 Therefore the garbage collector eventually deallocates the memory space.因此垃圾收集器最终会释放内存空间。

Just as Adam said, this is generally discouraged though, as it messes up your expectation that a constructor should initialize the object, and interactions with the outside world should be possible via its methods.正如亚当所说,这通常是不鼓励的,因为它破坏了您的期望,即构造函数应该初始化对象,并且应该可以通过其方法与外部世界进行交互。

So, even if it is a bit longer, better structured code could for example look like this:因此,即使它有点长,更好的结构化代码也可能如下所示:

class TestCode {
   public TestCode() {}

   public void welcome() {
      System.out.println();
      System.out.println("Welcome to the Program");
   }

   public static void main( String args[] ){
      TestCode tc = new TestCode();
      tc.welcome();
   }
}

PS Of course, there are cases where you might want to do operations outside the instance upon every instantiation anyway, for example like keeping a static list of all the instances of the class, and therefore adding the particular instance to it each time it is constructed. PS当然,在某些情况下,您可能希望在每次实例化时都在实例之外进行操作,例如,保留类的所有实例的静态列表,因此每次构造时都将特定实例添加到其中. Even then though, it is probably best to create a private init() method and invoke it from the constructor, as to better separate elements with different function.尽管如此,最好创建一个私有的 init() 方法并从构造函数调用它,以便更好地分离具有不同功能的元素。

As another example, here is a different, functionally identical way your code could be reformulated:再举一个例子,这里有一种不同的、功能相同的方式可以重新编写您的代码:

class TestCode {
   public TestCode() {
      init();
   }

   private void init() {
      System.out.println();
      System.out.println("Welcome to the Program");
   }

   public static void main( String args[] ){
      TestCode tc = new TestCode();
      // Or simply: new TestCode();
   }
}

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

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