Page 1 of 1

question about enthuware.ocpjp.i.v11.2.3201

Posted: Sat Jun 06, 2020 4:58 pm
by dimitrilc
Why is
A var declaration must have an initializer. Without the initializer, the compiler will not be able to deduce the type of the variable.
not a valid statement?

When I try to declare an empty var,

Code: Select all

public class CompileTimeConstant{
	public static void main(String[] args){
		var v1;
	}
}
compiler refuses to run and gives error,

Code: Select all

CompileTimeConstant.java:3: error: cannot infer type for local variable v1
                var v1;
                    ^
  (cannot use 'var' on variable without initializer)
1 error
error: compilation failed

Re: question about enthuware.ocpjp.i.v11.2.3201

Posted: Sat Jun 06, 2020 8:02 pm
by admin
This is indeed a valid statement. But it is the explanation to the option number 4, which is an incorrect option. It explains why option 4 is incorrect.

Re: question about enthuware.ocpjp.i.v11.2.3201

Posted: Sat Jun 06, 2020 9:15 pm
by dimitrilc
admin wrote:
Sat Jun 06, 2020 8:02 pm
This is indeed a valid statement. But it is the explanation to the option number 4, which is an incorrect option. It explains why option 4 is incorrect.
Thank you. You are right. I thought the blue text in review mode was part of the answer. I totally face-palmed myself there. :o

Re: question about enthuware.ocpjp.i.v11.2.3201

Posted: Thu Jun 11, 2020 1:44 am
by dimitrilc
I am not sure if this is out of scope for the exam or not, but //1 can also be wrong. The TestClass.java file won't run if you run it with "java" and complains that it cannot find the main method.

To run this file, you must compile it first with javac, then run with java.

Re: question about enthuware.ocpjp.i.v11.2.3201

Posted: Thu Jun 11, 2020 2:02 am
by admin
The problem statement isn't asking about running the code.