Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   trigger static init (http://www.velocityreviews.com/forums/t945243-trigger-static-init.html)

Roedy Green 04-09-2012 06:28 AM

trigger static init
 
What is the canonical way to explicitly trigger a static class to load
and run thestatic init without actually invoking a real method?

you can't necessarily use newInstance since the class may not have
constructors.

Do you have to invent some dummy static method?
--
Roedy Green Canadian Mind Products
http://mindprod.com
When you were a child, if you did your own experiment
to see if it was better to put to cocoa into your cup first
or the hot milk first, then you likely have the programmer gene..

Jukka Lahtinen 04-09-2012 07:32 AM

Re: trigger static init
 
Roedy Green <see_website@mindprod.com.invalid> writes:

> What is the canonical way to explicitly trigger a static class to load
> and run thestatic init without actually invoking a real method?


I suggest Class.forName(String classname), if you just want to avoid
invoking a method of the class to be loaded and don't mind invoking
one of the Class class.

If you insist on not invoking any method of *any* class and thus don't
want to use Class.forName, you can refer to some public static attribute
of the class.

--
Jukka Lahtinen

glen herrmannsfeldt 04-09-2012 09:18 AM

Re: trigger static init
 
Roedy Green <see_website@mindprod.com.invalid> wrote:
> What is the canonical way to explicitly trigger a static class to load
> and run thestatic init without actually invoking a real method?


> you can't necessarily use newInstance since the class may not
> have constructors.


As far as I know, a class automatically has a no argument constructor,
even if you don't write one.

class myclass {
static int x;
}

compiles just fine, as does creating an object from it.

> Do you have to invent some dummy static method?


Not that I know of.

-- glen

Roedy Green 04-09-2012 11:57 AM

Re: trigger static init
 
On Mon, 9 Apr 2012 09:18:04 +0000 (UTC), glen herrmannsfeldt
<gah@ugcs.caltech.edu> wrote, quoted or indirectly quoted someone who
said :

>As far as I know, a class automatically has a no argument constructor,
>even if you don't write one.


You can't always do that. The sole explicit constructor may be private
to explicitly stop you from instantiating objects.
--
Roedy Green Canadian Mind Products
http://mindprod.com
When you were a child, if you did your own experiment
to see if it was better to put to cocoa into your cup first
or the hot milk first, then you likely have the programmer gene..

Roedy Green 04-09-2012 01:09 PM

Re: trigger static init
 
On Mon, 09 Apr 2012 10:32:21 +0300, Jukka Lahtinen
<jtfjdehf@hotmail.com.invalid> wrote, quoted or indirectly quoted
someone who said :

>, you can refer to some public static attribute
>of the class.


But it had better not be a static final known at compile time, or it
will be optimised to a literal and referencing it won't load the
class.

I decided to handle it by putting in dummy public static void fireup()
methods that as a side effect load the class and run static init. If I
fail to call them, class loading is just procrastinated until first
use.
--
Roedy Green Canadian Mind Products
http://mindprod.com
When you were a child, if you did your own experiment
to see if it was better to put to cocoa into your cup first
or the hot milk first, then you likely have the programmer gene..

markspace 04-09-2012 04:19 PM

Re: trigger static init
 
On 4/9/2012 6:09 AM, Roedy Green wrote:

> I decided to handle it by putting in dummy public static void fireup()
> methods that as a side effect load the class and run static init. If I
> fail to call them, class loading is just procrastinated until first
> use.



This sort of idea sounds to me like the worst sort of hackish design
possible. Don't use statics for this use case. Use Factories, or
Strategies, or even just a Properties object if you only need to load a
few constants at runtime.



Lew 04-09-2012 05:31 PM

Re: trigger static init
 
glen herrmannsfeldt wrote:
> Roedy Green wrote:
> > What is the canonical way to explicitly trigger a static class to load
> > and run thestatic init without actually invoking a real method?

>
> > you can't necessarily use newInstance since the class may not
> > have constructors.

>
> As far as I know, a class automatically has a no argument constructor,
> even if you don't write one.
>
> class myclass {
> static int x;
> }


He meant no *accessible* constructors.

If the constructor is not accessible, its existence is moot.

And Java coding conventions call for type names to have an initial upper-case letter and be in camel case: 'MyClass'.

> compiles just fine, as does creating an object from it.
>
> > Do you have to invent some dummy static method?

>
> Not that I know of.


The rules for class initialization are quite explicitly listed in the JLS, which document is eminently useful no matter how much one may affect to loathe it.
<http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.2>

--
Lew

Lew 04-09-2012 05:33 PM

Re: trigger static init
 
Roedy Green wrote:
> Jukka Lahtinen wrote, quoted or indirectly quoted someone who said :
>>, you can refer to some public static attribute
>>of the class.

>
> But it had better not be a static final known at compile time, or it
> will be optimised to a literal and referencing it won't load the
> class.
>
> I decided to handle it by putting in dummy public static void fireup()
> methods that as a side effect load the class and run static init. If I
> fail to call them, class loading is just procrastinated until first
> use.


Two questions:

Why is it important to control when the class loads, instead of using the default?
What was wrong with 'Class#forName()', which is intended for the purpose and is not a hack?

--
Lew

Lew 04-09-2012 05:36 PM

Re: trigger static init
 
Jukka Lahtinen wrote:
> Roedy Green writes:
>> What is the canonical way to explicitly trigger a static class to load
>> and run thestatic init without actually invoking a real method?

>
> I suggest Class.forName(String classname), if you just want to avoid
> invoking a method of the class to be loaded and don't mind invoking
> one of the Class class.
>
> If you insist on not invoking any method of *any* class and thus don't
> want to use Class.forName, you can refer to some public static attribute
> of the class.


Bear in mind that the 'class' literal does not apply here. Referencing the 'class' literal does not incur class initialization.

--
Lew

Daniel Pitts 04-10-2012 07:22 PM

Re: trigger static init
 
On 4/8/12 11:28 PM, Roedy Green wrote:
> What is the canonical way to explicitly trigger a static class to load
> and run thestatic init without actually invoking a real method?
>
> you can't necessarily use newInstance since the class may not have
> constructors.
>
> Do you have to invent some dummy static method?

If you have to worry about this problem, then you've got the wrong design.

Classes always have a constructor, but they may not have a Default
constructor, or the constructor may be private.

In any case, if you know about the class specifically before hand, then
you probably have the wrong design, and should convert it to a
non-static class. Probably you want dependency injection if you need
only one. Perhaps as a singleton if you *really* need that, but only as
a last resort.





All times are GMT. The time now is 04:36 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.