Interface and Constants

G

Gyoergy Magoss

Hi,

I have a question regarding interfaces and constants.
I have defined a constant within an interface :

public interface InitInterface {

public final static boolean INITIALIZED = Initializer.initAll() ;
}


The main thing is. I want Initializer.initAll() to run autmatically
when a class is loaded, that implements this interface , e.g.

public class InitializerTest implements InitInterface {
public InitializerTest() {
super() ;
}
}

Alas the constant is not initialized when loading the class into the
classloader but rather when I first access the constant, e.g. by
adding the line
System.out.println("Initialized : " + INITIALIZED);
(by the way as soon as I do that and there are several constants in
the interface, all of them not only the one accessed are
initialized...)
to the constructor. My question : Is there a way to force a VM to
initialize the constants during class loading ?

Thanks

György
 
A

ak

You cant call any method from interface.
If you want to run it only one time when a class is loaded then it must be
static, and in interface you can't define static things.
 
M

michael

hi,

You could write a servlet (the most time named "controller servlet"),
which will be loaded on the start up of the webapp.

In the web.xml, just put :

<servlet>
<servlet-name>MyController</servlet-name>
<servlet-class>mypackage.MyController</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

And in your servlet MyController, in the void init() method, call your
class to initialize your variables.

Hope it helps :)
 
D

David Zimmerman

Gyoergy said:
Hi,

I have a question regarding interfaces and constants.
I have defined a constant within an interface :

public interface InitInterface {

public final static boolean INITIALIZED = Initializer.initAll() ;
}


The main thing is. I want Initializer.initAll() to run autmatically
when a class is loaded, that implements this interface , e.g.

public class InitializerTest implements InitInterface {
public InitializerTest() {
super() ;
}
}

The constant will be initialized when the interface is loaded. This
wiull happen as soon as you reference one the constants or of you
reference an object that implements the interface as an instance of that
interface. You will need to put that constant in every class for which
you want to call initAll()
 
?

=?ISO-8859-1?Q?Gy=F6rgy_Magoss?=

ak said:
You cant call any method from interface.
If you want to run it only one time when a class is loaded then it must be
static, and in interface you can't define static things.
Hi, whoever you are. Off course you can define "static things" within
an interface. If you define a field in an interface it automatically
is "public final static". And since even final fields can be assigned
the result of a method (once!), you can as such call any method from
an interface (as you put it!). The only thing is. The message may not
be part of the interface (in this case the method is a static method
from another class!).
That was (and is) not the question. The question was : Why is the
static field not initialized on load time but rather when I first
access it ?

I do not want to sound rude. But it would be very helpful if you would
take the time to read the questions before replying them and not just
browse for key words and say that it does not work.
 
D

Dale King

ak said:
You cant call any method from interface.
If you want to run it only one time when a class is loaded then it must be
static, and in interface you can't define static things.


Actually you can. It can't be an instance method of the interface, but could
be a static method or an instance method on an object whose reference you
can get from a static context or even a constructor.
 
G

Gy?rgy Magoss

hi,

You could write a servlet (the most time named "controller servlet"),
which will be loaded on the start up of the webapp.

In the web.xml, just put :

<servlet>
<servlet-name>MyController</servlet-name>
<servlet-class>mypackage.MyController</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

And in your servlet MyController, in the void init() method, call your
class to initialize your variables.

Hope it helps :)


No, that did not help. I don't have a servlet engine in this case. The
idea was to have a chron job opening a JVM running this class in
certain intervalls. There is no application server installed on that
machine.
Thank you nevertheless.

Gyoergy
 
G

Gy?rgy Magoss

David Zimmerman said:
The constant will be initialized when the interface is loaded. This
wiull happen as soon as you reference one the constants or of you
reference an object that implements the interface as an instance of that
interface. You will need to put that constant in every class for which
you want to call initAll()


Thank you very much for the information. This is what I observed but I
hoped there would be a way around it.

Gyoergy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,156
Latest member
KetoBurnSupplement
Top