Practice mode Java

Where is the reserved type name var not allowed in Java?

Correct answer C. As the declared type of a field

Explanation

Local variable type inference with var applies to local declarations that have an initialiser, including for-loop variables and try-with-resources resources. Fields, method parameters and return types must state their types explicitly, because they form part of a class's visible contract. Writing var count = 0; as a class member is a compile-time error.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.