Think of it like this...
If you don't want to initialize your variable, you can just declare it:
MyObject myObject;
However, if you want to be able to do anything at all with myObject
, you need to create an object that the variable can point to, and that is done with the new
keyword. Otherwise myObject
is just pointing to a big nothing.
Over the years, the terminology in Java's upstream languages intrudes on the Java language.
In the past (C) allocation was a separate step from initialization. They combined in C++, and now are considered the same thing. Technically, the order of operations in "construction" is
These steps may be done multiple times for parent classes of the object. The key is that allocation always precedes initialization, and the two are tightly coupled. Allocation must precede initialization, or the initialization has no memory to initialize. Constructors guarantee both are done to avoid earlier issues in other languages where programs would access random data by accessing allocated but not initialized items. Constructors were created to protect against such bugs.
Now, the stuff you assign to is a variable, and variables hold references to values (in Java, for objects). So you can consider a variable simply a name that can be dereferenced to a value. If you want a new name, but want to have it reference nothing, then Keppil's answer is best.
MyType myName;
which will create a name with a reference to no value (null).
However, in Java, object values must be initialized. There is no "allocate without initialization", as this would be the equivalent to having an object with random internal state. Random internal state means that using the object would have (at best) random results, and (at worst) references to objects that didn't exist. With the tight coupling in construction of allocation and initalization, the type system works; without it, you could theoretically have a reference to an object that doesn't exist, recreating the segmentation fault of C in Java.
You want to initialize the object itself, which includes allocating memory for the object. There's other housekeeping internals that need to be done as well.
To quote the link you posted:
The job of the constructor is to ensure that the new object is in a valid state ...
Do you really want to create an object and not have it in a valid state?
If you however just want an object reference, just declare one and do not call new.
MyClass mc;
You are right, Java syntax could be
String str = new
instead of
String str = new String()
to give you an empty string. The problem is sometimes we want to pass parameters into a constructor like this:
String str = new String("text")
which is why we call the constructor. To keep the syntax consistent, you have to also call default constructors String()
. In this case String
is an immutable class so there is no other way to set it's initial value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.