• 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

non-static block

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the significance of a non-static block? Is there a difference When we just put this code(whatever is the non-static block) in the constructor itself?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use it to execute code that must be run in all constructors without needing to copy that extra code. You can use a private method for that but you may forget to add the call to that method if you add a constructor.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are there any style/usage guidelines for doing this? I used to get yelled at because it wasn't "clear" what was happening. My response was "then you need better Java programmers that actually know the language", but that went over as well as you'd expect. And I refused to put a comment above it saying what it was or when it ran >:|
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally I think initializer blocks are obscure enough to avoid in most code. They're handy for initializing anonymous classes and in a few other special cases, but I'd tend to agree that there are clearer alternatives in most cases. Unit testing avoids Rob's drawback to common init methods.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But... Java is restrictive enough; I'm really, *really* loathe to avoid what few features it has. Things are obscure only because they're not used, and I'm uncomfortable using that as a reason to avoid standard functionality.
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:And I refused to put a comment above it saying what it was or when it ran >:|


Teamplayer?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's called an instance initializer. Instance initializers are rarely used, you don't really need them in general.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Peter Taucher wrote:Teamplayer?


Overly-optimistic.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:I used to get yelled at because it wasn't "clear" what was happening. My response was "then you need better Java programmers that actually know the language", but that went over as well as you'd expect.




Peter Taucher wrote:

David Newton wrote:And I refused to put a comment above it saying what it was or when it ran >:|


Teamplayer?


If someone is asking me to dumb down my code because others don't know the language well enough (or sometimes aren't smart enough) to read it, then stuff 'em! Why should I adapt when making the others adapt would be much better in every aspect? They get to be smarter, my code stays as smart as it was, and most importantly, I don't have to do any extra work
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can follow your arguments ... BUT ; - ) a little comment here and there for the less professional developers in your team may not hurt that much.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm just not going to comment basic language features; I'd rather use an init method that doesn't need one.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Peter Taucher wrote:I can follow your arguments ... BUT ; - ) a little comment here and there for the less professional developers in your team may not hurt that much.


I'm not against documenting or using no comments at all, but just not for basic language features, as David said.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic