Hello everyone. The following seems a common idiom:
public static void main(
String... args) {
new Cat().meow();
}
We instantiate an object of a class, then invoke a method of the class.
What concerns me is that I have read that the precedence of the "." operator is higher than the precedence of the "new" operator. Assuming that is true, then how can the above code work? I think we would need to do "(new Cat()).meow()". Except, of course, we all know that the above code really works.
I thought of this because of the following. (Note that class Cat extends Animal.)
new Cat().meow();
Cat c = new Cat();
Animal a = c;
(Cat) a.meow();
The compiler says that last line is "not a statement". This makes sense to me, since "." has higher precedence than the cast operator. ((Cat) a).meow(); works fine.
So I don't have a solid uinderstanding of why "new Cat().meow()" works, and we do not need to say, "(new Cat()).meow();"