Given myClass
below and the non-static method run()
, the following line of code is valid:
new myClass().move();
However, this is also valid:
move();
I understand the first attempt (new myClass().move()
) creates an instance of the class and then calls the method on it. Does the second attempt also do the same thing but implicitly? In other words, are the 2 attempts really the same? If not, what is the fundamental difference and which one is preferred?
It seems OscarRyz's comment in the post here touched on this but he did not elaborate.
class myClass
{
void move() {
//...some code
}
void run() {
new myClass().move();
}
}
Thanks.