Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

non-static block

 
Rajesh Nagaraju
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • 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?
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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 >:|
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • 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?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Taucher wrote:Teamplayer?

Overly-optimistic.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic