Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt on instance initializer and constructor.

 
Ritesh raushan
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i hava a some doubt.

1-why interface need a final variable.(what's the use of variable in interface)

2-what is the use of instance initializer even constructor is already for intialization
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It used to be thought it was useful to put constants in interfaces so they would be available to implementing classes. More modern opinions: try here.
About initialisers: I don’t like them; they are called regardless of which constructor is invoked, so that might be a use.
 
Paul Clapham
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:About initialisers: I don’t like them; they are called regardless of which constructor is invoked, so that might be a use.


And if you have a situation (like in an anonymous inner class) where constructors cannot be used, then instance initializers are essential. (However, even though I use anonymous inner classes extensively, I don't believe I have ever found it necessary to put an instance initializer in one.)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic