Create an empty object in Java?

How did you do it? It's so simple:

Name myName = new Name();

I'm a little confused It should be a class without instance variables I just need to "create an empty object" The constructor is, of course, empty

Solution

If name has a parameterless constructor, it must be Whether it is "empty" depends on the function of the constructor or the default value it may have

How do you define "empty object"?

For example, if you want a variable but don't want it to actually own an object, you just declare the variable without initializing it:

Name myName;

In this case, myname will be null, but its type is name, which can be used later (once it is assigned a value)

All variables themselves point to where an "object" exists in memory Therefore, things like the name myname do not "create" an object, it just creates a pointer to a memory location New name () actually creates an object by calling its constructor When used like your example, the second half creates the object, and then the first half points to the location in the object's memory

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>