Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > trigger static init

Reply
Thread Tools

trigger static init

 
 
Roedy Green
Guest
Posts: n/a
 
      04-09-2012
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..
 
Reply With Quote
 
 
 
 
Jukka Lahtinen
Guest
Posts: n/a
 
      04-09-2012
Roedy Green <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      04-09-2012
Roedy Green <(E-Mail Removed)> 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-09-2012
On Mon, 9 Apr 2012 09:18:04 +0000 (UTC), glen herrmannsfeldt
<(E-Mail Removed)> 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..
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-09-2012
On Mon, 09 Apr 2012 10:32:21 +0300, Jukka Lahtinen
<(E-Mail Removed)> 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..
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      04-09-2012
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.


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      04-09-2012
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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      04-09-2012
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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      04-09-2012
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
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      04-10-2012
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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
init of class members : mem(0) vs. mem() vs. not-init at all news.aon.at C++ 11 01-29-2011 07:30 PM
Trigger function on shared module init rayuthar@gmail.com C Programming 2 04-17-2008 10:34 AM
questions about object initialization, default-init and value-init Jess C++ 4 05-04-2007 02:47 AM
Sequence Order between Page Init and User Control Init Tony Cheng ASP .Net 1 02-24-2006 01:56 PM
Compiler/Linker Error undefined reference to 'std::ios_base::Init::Init[in-charge]() clusardi2k@aol.com C++ 1 08-18-2005 07:11 PM



Advertisments