aspose file tools*
The moose likes Beginning Java and the fly likes non-static block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "non-static block" Watch "non-static block" New topic
Author

non-static block

Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 62
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

Joined: Oct 27, 2005
Posts: 19784
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Nov 18, 2006
Posts: 174
David Newton wrote:And I refused to put a comment above it saying what it was or when it ran >:|

Teamplayer?


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

It's called an instance initializer. Instance initializers are rarely used, you don't really need them in general.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Peter Taucher wrote:Teamplayer?

Overly-optimistic.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

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

Joined: Nov 18, 2006
Posts: 174
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 27, 2005
Posts: 19784
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: non-static block