• 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

Static initializers

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the purpose of these? It seems as though their purpose is redundant (and dangerous) because of the existence of constructors.
For example:
class test{
String foo;
public static void main(String[] argv){
test t = new test(){
{foo = "FUBAR";}
};
}
}
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sophtwarez
Welcome to the Java Ranch, we hope you’ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, you dont have a static initializer in your code anywhere.
The initializers (static or instance) would only be redundant if your typed them twice - once in the constructor and once in the initializer. On the other hand that is the main purpose of having them in the first place is to be able to reuce typeing and initialize variales in one place instead of repeating it over and over in each of your constructors.
As far as being dangerous - even of you do repeat your initialization the constructor is completed after any initialization blocks so the values assigned there would be the ones you end up with.
hope that helps
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(non-Static) initializers exist so that anonymous classes - which obviously can not have a constructor because they have no name - still have a way of get initializing an object of the class.
(I put the Static word in parens to correct the incorrect statement that I made earlier. Of course Jim is correct.) (sigh).
[ February 21, 2002: Message edited by: Cindy Glass ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ummm... a static initializer would execute once for an entire class. Its main use is to initialize class variables which require more than one line of code to initialize - e.g. if you must do error handling on initialization. You can't do something like this in the declaration statement, and there is no constructor at the class (static) level - only at the instance level. The static initializer is essentially a replacement for a class constructor - doing anything that needs to be donw with the static fields of the class.
A (non-static) instance initializer would be similar to a constructor, but except it can be used for anonymous classes. I presume this is what Cindy was talking about.
In the original code, the line
{foo = "FUBAR";}
is not any kind of initializer, since it is inside a method. It is an unlabeled block. I can't see any purpose to making a block out of it here - the code would execute the same if the {} were removed. Unlabeled blocks can be useful in only one case I can think of: they can be used to limit the scope of a variable, so you can re-use it elsewhere in the same method without confusion. A labeled block could have additional potential usages as the target of a break statement, e.g.

It is extremely rare that this type of usage would be good programming practice. As near as I can tell it could always be converted to something equivalent and clearer, e.g.:
 
Tetsuo Suzuki
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim. I think I understand static initializers but how about instance initializers? Going back to what Cindy said, how can an anonymous class *not* have a constructor? Constructors are called with the new keyword, not a class name.
Perhaps I'm a bit dull on the edges but isn't an anonymous class something like this:

In this case the anonymous class still was initialized with values using a constructor.
No need for an "instance initializer".
 
Tetsuo Suzuki
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just got it.

Interesting feature of the language.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cindy .. . Cindy (Cindy slowly shakes head) :roll:
I tweaked the silly statement that I made earlier so as to not confuse anyone further with such a statement.
(Note to self: READ what you type before posting!)
[ February 21, 2002: Message edited by: Cindy Glass ]
 
It's feeding time! Give me the food you were going to give to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic