• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about Mock Exam 4, question 13 (Java OCA 8 Programmer I Study Guide)

 
Poyan Gerami
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it should be 3 compile errors ?
"variable bench might not have been initialized"

they have count line 4 and 15 as compile errors, should be just line 4 ?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Poyan Gerami wrote:it should be 3 compile errors ?
"variable bench might not have been initialized"

they have count line 4 and 15 as compile errors, should be just line 4 ?

No! I agree with the book! 4 compiler errors it is. And more importantly, my Java compiler agrees with me (and the book) as well

I don't think there is any discussion about lines 11 and 12: both initialize a final variable for the second time which will result in a compiler error (count = 2). Because bench is not initialized on line4 and it's not initialized in one of the static initializer blocks as well, you have a final class/static variable which is not initialized at all. That's a compiler error as well (count = 3). And finally on line15 final class/static variable bench is initialized in a method, which is again not allowed and will result in another compiler error (count = 4). You can only initialize a final class/static variable instantly (meaning on the same line as the declaration) or in a static initializer block; all other locations/attempts will result in a compiler error!
Here you'll find a nice overview of how to initialize class/static variables, instance variables and local variables. I would recommend reading it as it's very informative (with a code example as well).

Hope it helps!
Kind regards,
Roel
 
Poyan Gerami
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this test give me just one error:



Test.java:13: error: cannot assign a value to final variable i
i = "test";
^
1 error
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Poyan Gerami wrote:this test give me just one error:

That's indeed a compiler error Now either remove the line i = "test"; or comment this line out and try to compile the Test class again and see what happens.
 
Poyan Gerami
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you get another compile error when you remove i = "test";
But the compiler give us only 1 compile error when having both not valid statements, and the question was how many compile error does this code give. So in the exam we need to check all possible compile errors.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Poyan Gerami wrote:But the compiler give us only 1 compile error when having both not valid statements, and the question was how many compile error does this code give. So in the exam we need to check all possible compile errors.

In the code you provided there are 2 compiler errors:
1/ final class/static variable i is not initialized instantly or in a static initializer block
2/ final class/static variable i is initialized in a (static) method

Both are not allowed, both will result in a different compiler error. So if you get this question on the exam, you have to select 2 compiler errors, because that's simply how many compiler errors this code has.

Hope it helps!
Kind regards,
Roel
 
Poyan Gerami
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my compiler just give me 1 error when compiling the code I provided.
 
Ankit Garg
Sheriff
Posts: 9577
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Poyan Gerami wrote:my compiler just give me 1 error when compiling the code I provided.

When you fix the 1st error you'll get the second error. So you need to make 2 changes for the code to compile, thus the code has 2 compilation errors. The compiler might report all errors at once or second after you fix the first that doesn't matter, you need to identify how many changes are required for the code to compile successfully...
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:
Poyan Gerami wrote:my compiler just give me 1 error when compiling the code I provided.

When you fix the 1st error you'll get the second error. So you need to make 2 changes for the code to compile, thus the code has 2 compilation errors. The compiler might report all errors at once or second after you fix the first that doesn't matter, you need to identify how many changes are required for the code to compile successfully...

Exactly! That's what I tried to explain in my previous post, but I clearly didn't succeed
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic