• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Non-static initializer vs Constructor

 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The non-static initializer is the only way to do initialization in an anonymous inner class, as those can't have constructors.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ilja....
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
kenji mapes
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. Learn something new everyday here. I am amazed at your guy's wealth of knowledge.
 
Mahesh x Bogadi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I could not understand.

Please add a coding example!!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic