• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt on instance initializer and constructor.  RSS feed

 
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
Posts: 53779
128
  • 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: 22185
38
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
Boost this thread!