• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my compiler just give me 1 error when compiling the code I provided.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
We noticed he had no friends. So we gave him this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic