• 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 initializer vs Constructor

 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ilja....
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow. Learn something new everyday here. I am amazed at your guy's wealth of knowledge.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I could not understand.

Please add a coding example!!
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
This is my favorite show. And this is my favorite tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic