简体   繁体   English

在java中使用new关键字而不定义变量

[英]Using new keyword without defining variable in java

The following code is used to create an object of a class within a variable:以下代码用于在变量中创建类的对象:

ClassName obj = new ClassName();

But what if we do not create any variable and just type the following?但是如果我们不创建任何变量而只输入以下内容呢?

new ClassName();

When I used it, there was no error.当我使用它时,没有错误。 But what it actually does when no variable is created?但是当没有创建变量时它实际上做了什么?

Both methods create an object, but the second method has no name for it.这两种方法都创建了一个对象,但第二种方法没有名称。

When you create an instance of the ClassName, the difference between your first method and second method is that for your first method, you can actually access variables and methods within the class.创建 ClassName 的实例时,第一个方法和第二个方法之间的区别在于,对于第一个方法,您实际上可以访问类中的变量和方法。

For example,例如,

ClassName obj = new ClassName();
System.out.println(obj.x);

Where ClassName哪里类名

class ClassName {
    public static int x = 2;
}

The benefit of this approach is you can access the variables within the class.这种方法的好处是您可以访问类中的变量。 If you used the second approach, then the instance would have no name, so you wouldn't be able to access the variables.如果您使用第二种方法,则实例将没有名称,因此您将无法访问变量。

new ClassName() calls a special method called a constructor and, like any method, it returns a value 1 . new ClassName()调用称为构造函数的特殊方法,并且与任何方法一样,它返回值1 The value that it returns is an instance of ClassName .它返回的值是ClassName一个实例。 You can choose to assign that value to a variable, or not, just as you can do with the return value of any method.您可以选择是否将该值分配给变量,就像您可以处理任何方法的返回值一样。 So you simply need to decide whether you need to assign the returned value to a variable, or not.因此,您只需要决定是否需要将返回值分配给变量。 If you do, then you need the first statement, namely:如果你这样做,那么你需要第一个语句,即:

ClassName obj = new ClassName();

And if you don't need to assign the returned value to a variable, then you can use:如果您不需要将返回值分配给变量,则可以使用:

new ClassName();

1 - a method can return void which means it doesn't actually return a value 1 - 方法可以返回void ,这意味着它实际上并不返回值

To answer to your question I think is useful to see how objects are managed by JVM during execution.为了回答您的问题,我认为了解 JVM 在执行期间如何管理对象很有用。

All java objects reside in a JVM area called heap and can ( note that this is not mandatory ) be pointed by one or more variables.所有 java对象都驻留在称为的 JVM 区域中,并且可以(注意这不是强制性的)由一个或多个变量指向。 The variables that hold references to objects reside on another area of JVM called stack .保存对象引用的变量位于 JVM 的另一个区域,称为stack

Every time an instruction like new SomeClass();每次像new SomeClass();这样的指令is executed a new object is allocated on that area.执行时,会在该区域上分配一个新对象。 When the heap area becomes full, garbage is collected and during the garbage collection objects that are no longer pointed by a variable are cleared in order to free space on heap for new objects.当堆区域变满时,垃圾被收集,并且在垃圾收集期间不再由变量指向的对象被清除,以便为新对象释放堆上的空间。

What is the difference between your two instructions?你的两个指令有什么区别?

在此处输入图片说明

1- ClassName obj = new ClassName(); 1- ClassName obj = new ClassName();

It allocates a new object on heap that is pointed by the obj variable on stack.它在堆上分配一个新对象,该对象由堆栈上的obj变量指向。

2- new ClassName(); 2- new ClassName();

It just allocates a new object on heap with no variables on stack that point to it.它只是在堆上分配一个新对象,堆栈上没有指向它的变量。

When can be useful to use the second approach?什么时候可以使用第二种方法?

According to this, using the second approach could be useful if you want just to allocate a new object on heap without using it through a variable for example to test memory management on your program.据此,如果您只想在堆上分配一个新对象而不通过变量使用它,例如测试程序上的内存管理,则使用第二种方法可能会很有用。 Actually the second approach is used when you want to create a "one-shot" object and use a method or a variable of it in the same instruction improving the garbage collector efficiency and avoiding unexpectedly get an OutOfMemoryError exception like:实际上,当您想创建一个“一次性”对象并在同一条指令中使用它的方法或变量时使用第二种方法,以提高垃圾收集器的效率并避免意外获得 OutOfMemoryError 异常,例如:

  • using once time an object variable:一次使用一个对象变量:
System.out.println(new ClassName().x)

or或者

  • executing once time an object method:执行一次对象方法:
new ClassName().evaluateResult();

NOTE: In this specific case you probably could evaluate instead to make a static method on the class.注意:在这种特定情况下,您可能可以评估以在类上创建静态方法

Another usage can be to put new elements on a collection as follow:另一种用法是将新元素放入集合中,如下所示:

final List<ClassName> objectList = new ArrayList<>();
for(int i=0; i<10; i++){
  objectList.add(new ClassName());
}

You can may ask: What happens instead to objects that are pointed by at least one variable like ones created with first approach?您可能会问:对于由至少一个变量指向的对象(如使用第一种方法创建的对象)会发生什么?

All references that are held in a variable are naturally dropped when the variable goes out of scope so they became eligible for garbage collection automatically ( according how their scope is managed on code ).当变量超出范围时,变量中保存的所有引用都会自然删除,因此它们自动符合垃圾收集条件(根据它们的范围在代码上的管理方式)。

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

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