Page 1 of 1

About Question enthuware.ocpjp.i.v11.2.3200 :

Posted: Sat Mar 07, 2020 12:29 pm
by Dani1515
private var x = 10; // a wrong option

"Since 10 is an integer value, compiler will deduce the type of x to be int. However, var declarations can only be used inside a method and for loop declarations. So, the modifier private makes this option invalid." thats the description why this is wrong

according to "the book" from Mr. Deshmukh page 88: var str1 ="hello1"; is valid in static as well as intance initializers
so private var x=10; should work in instance initializers

badly i can not test it, because my jdk is on version 8 something, so im going to update it now and check it also.

Re: About Question enthuware.ocpjp.i.v11.2.3200 :

Posted: Sat Mar 07, 2020 12:57 pm
by Dani1515
ok checked it, it works in intance and static initializer, but just like a local variable, and thats why "private var x=10; will not work,
var x=10; would work

Re: About Question enthuware.ocpjp.i.v11.2.3200 :

Posted: Sat Mar 07, 2020 9:55 pm
by admin
Right, var declarations cannot be used while declaring class/instance fields. The explanation has been enhanced to make this point clear.
thank you for your feedback!

Re: About Question enthuware.ocpjp.i.v11.2.3200 :

Posted: Fri Apr 17, 2020 2:54 am
by BaxLi007
I got the idea with the question/answers ...
but i thought when I made my wrong answer "final int x " is unchangeable, so it a constant not a variable ...