• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

OCPJP 7 ( local inner class )

 
Greenhorn
Posts: 29
Mac Java ME Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey everybody

i m preparing for the certification OCPJP 7 and i fiend in the book " A local class can access all the variables available in the body of the code in which it is defined.You can pass only final variables to a local inner class. " ( book Oracle Certified Professional Java SE 7 Programmer Exams 1Z0-804 and 1Z0-805 )

but the cod test i make in eclips give me other things :

la valeur de x 3 la valeur de y 4 la valeur de z 5 la valeur de e 6


can some one tell me how he is right ?
 
adil zahir
Greenhorn
Posts: 29
Mac Java ME Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry i found the solution :

the question why x must be final ?



as i think because x it local variable and local variable go out of memory when the cod of method finish , so for inner class can stil acesse to variable must be in the heap for that we need to make it final !!!


 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right. The reason for this is the object created in the method could outlive that method. Local method variable would be gone by then.

Funny thing ;). Your code would work on Java 8. They relaxed a requirement for final. In Java 8 the variable need not be marked as final but it must be effectively final (not modified) in order to be used in inner class.
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic