I was studying Java thread and created an object.
SoloThread s = new SoloThread(10);
s.start();
I abbreviated this part
new SoloThread(10).start();
I've seen a case where I only use it.
I've seen a few more, even if it's not in the example above
Can I assign it by making a new one without a declaration?
I'm asking you because I'm curious about how the above code is possible!
java object syntax
A variable is just a symbol that points to the address (reference) of the object. The variable is used to comfortably import the address you are pointing to.
Without having to get the address of the object through variables, you can easily access the object if there is a way to procure the address.
new
is responsible for creating an object and returning its address.
Therefore, new SoloTread(10)
generates SoloTread in memory and returns its address.
At this time, the address can be accessed immediately without storing it in a variable, so new Solothread (10).An object created, such as start();
, can be used immediately.
In a language such as C++, you must release objects created with new
with delete
, but in the case of Java, the Garbage Collector (GC) exists, so it is released on its own.
© 2024 OneMinuteCode. All rights reserved.