I would like to know if somebody knows exactly what happens when the line of code comes to play:
class Class{}
class Math extends Class{}
class UseClasses{
public static void main (String[]args)
{
new Math(); //Line 8
(Class)new Math();// Line 9
}
I thoroughly understand that the "new keyword" serves as object instance creator in the heap memory. However in the preceding bit of code you can see that line 9 makes an unusual use of this keyword (new). Line 8 it's ok and compiles fine. but line 9 requires to assign the content to some other references. Thus this implies that each time the cast operand is present, in this case (Class)new Math, a new reference(underscoring reference and not object) is going to be instantiated.
Does it work in this way? If not, could you explain to me why it compiles fine in line #8 and it gives an error in line #9? (obviously due to the casting function not to be put there but why not?)