This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Non-static initializer vs Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Non-static initializer vs Constructor" Watch "Non-static initializer vs Constructor" New topic
Author

Non-static initializer vs Constructor

A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi all,

Both non-static initializer block and a constructor are xcuted during instance creation...u can set values in these blocks..


Then when should we make use of non-static initializer block..

And whats the difference between non-static block and constructor..?

Thx
A Kumar
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The non-static initializer is the only way to do initialization in an anonymous inner class, as those can't have constructors.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Thanks Ilja....
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
Good one, Ilja!

Also, you might have a class with a dozen constructors, each of which begins with the same lines. If you factor those lines out into a non-static initializer, you guarantee that all constructors will call them in the right order. Even constructors that haven't been written yet.

-- Phil


Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
kenji mapes
Ranch Hand

Joined: Jun 16, 2005
Posts: 38
Wow. Learn something new everyday here. I am amazed at your guy's wealth of knowledge.
Mahesh x Bogadi
Ranch Hand

Joined: Jul 06, 2004
Posts: 51
Sorry I could not understand.

Please add a coding example!!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Philip Heller:

Also, you might have a class with a dozen constructors, each of which begins with the same lines. If you factor those lines out into a non-static initializer, you guarantee that all constructors will call them in the right order. Even constructors that haven't been written yet.


I typically prefer constructor chaining for that, but I suppose there are examples where this doesn't work well.

I'd like to see an example, too!
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
Here's an example of what I mean:


The DataOutputStream and the Socket need multi-line initializer code because presumably their initialization involves calls that throw exceptions. The 3 constructors have no overlapping args, which is what makes this case a candidate for this approach.

Without a non-static initializer, we would have something like:



And it just gets stickier if there are more ctors.

-- Phil
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Philip Heller:
Here's an example of what I mean:

[...]

Without a non-static initializer, we would have something like:




That would be bad. But it can be made better:



Granted, the initializer has the advantage that you can't forget to call it. But it's probably more a matter of style, isn't it?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Ilja Preuss:

<hr></blockquote>

That would be bad. But it can be made better:



Granted, the initializer has the advantage that you can't forget to call it. But it's probably more a matter of style, isn't it?[/QB]


I like Ilja's alternative better since it avoids the possibility of calling the ctor with t set to null but s as a valid reference.

Layne


Java API Documentation
The Java Tutorial
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
Yep, it comes down to style ... for both approaches there are good and bad situations. For my personal style, the non-static initializer says, "Never ever construct without doing the stuff in the initializer."
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Layne Lund:

I like Ilja's alternative better since it avoids the possibility of calling the ctor with t set to null but s as a valid reference.


Just for clarification: I don't think anyone said the first constructor chaining example was a good idea. The alternative we were assessing was the use of instance initializers.
Dao Kia
Greenhorn

Joined: Sep 16, 2005
Posts: 2
Hello everyone,

Looking at this post, I have a question coming up in my head.

If I have a class like this:

class Test {

FileWriter fw;
{
try {
fw = new FileWriter("das.txt");
}
catch (IOException ioe){

}
}

// And some other code
}

It compiles fine. But how can I NOT catch the IOException and let the caller to handle it? What would be the code looks like?

Thank You,

Dao
 
Consider Paul's rocket mass heater.
 
subject: Non-static initializer vs Constructor
 
Similar Threads
Initializers
why we can not have synchronized block outside a method
What is the order of Initialization
Static Block
Constructor usage in superclasses