• 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

Final Members Doubt

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class My {
final int i;
}

why it give an error ... as it is mentioned that instance variable are initialized at the time of object creations . So it will get 0 ... nothing wrong .

one more thing , why variables of interface are implicitly public , static &
final . as interfece dont have constructor ... so when they will be initialized if we have not done at the time of declaration .

thanks .
 
bronco
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure is you're asking why things were done the way they were in the languauge or if you're just asking how things work...so this may not be helpful.

With the final instance variable in the class, the rule is that you have to assign a value to the variable in every constructor if you don't define it when the variable is declared. In your example, the (invisible) default constructor doesn't do this, so you get an error. The simple solution here would be:


or

As for the interface question, the only type of variables allowed in an interface are "constants" (static finals). As you say, there are no constructors, so you must init any such variables when you declare them:

 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks dave .
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic