Static and Instantiation

J

Jason Cavett

If I make a static class (AKA - everything in a class is static), does
that mean this class is loaded into memory as soon as the JVM starts
(assuming the class is being used somewhere within the application),
or is it lazily instantiated and not loaded into memory until it
actually needs to be used?

I searched around, but couldn't find the information. Thanks.
 
J

Joshua Cranmer

Jason said:
If I make a static class (AKA - everything in a class is static), does
that mean this class is loaded into memory as soon as the JVM starts
(assuming the class is being used somewhere within the application),
or is it lazily instantiated and not loaded into memory until it
actually needs to be used?

I searched around, but couldn't find the information. Thanks.

Class files are loaded on demand.
 
M

Mark Space

Jason said:
If I make a static class (AKA - everything in a class is static), does
that mean this class is loaded into memory as soon as the JVM starts
(assuming the class is being used somewhere within the application),
or is it lazily instantiated and not loaded into memory until it
actually needs to be used?

I searched around, but couldn't find the information. Thanks.


Probably the lazy instantiated one, but if it's critical you may not
want to count on it.

The default JVM loader does not load or initialize classes until needed,
but if your class goes through a JIT optimizer, your application just
might get all initialized and loaded right at start up, depending on how
the JIT compiler "sees" your code.

The best way to do this would probably be to count on lazy instantiation
(it'll work 99% of the time) but leave enough instrumentation in the
code that you can tell if it ever doesn't happen. In other words,
collect data from the running program. If you see performance drop and
the static class is the issue, you can tackle the problem then.

I'd log the static init procedure, and log just before you use it. So,
if you see the class getting initialized, but never see the "use" log
message, something happened to the lazy init feature.
 
J

Jason Cavett

Probably the lazy instantiated one, but if it's critical you may not
want to count on it.

The default JVM loader does not load or initialize classes until needed,
but if your class goes through a JIT optimizer, your application just
might get all initialized and loaded right at start up, depending on how
the JIT compiler "sees" your code.

The best way to do this would probably be to count on lazy instantiation
(it'll work 99% of the time) but leave enough instrumentation in the
code that you can tell if it ever doesn't happen.  In other words,
collect data from the running program.  If you see performance drop and
the static class is the issue, you can tackle the problem then.

I'd log the static init procedure, and log just before you use it.  So,
if you see the class getting initialized, but never see the "use" log
message, something happened to the lazy init feature.

Alright. Actually, the laziness of instantiation isn't the problem.
It's actually the reverse. I don't want everything loaded.

The reason for this is I'm developing a Java library for some other
applications, and it is rather large. I wanted to make sure that the
few classes that I made static wouldn't get loaded just by the sheer
fact that someone was using another component in the library that had
nothing to do with that class.

Intuition told me that it wouldn't make sense to load all the static
"stuff" up front (although, good point about the JIT compilers), but I
wanted to check to make sure.

Thanks for the response.
 

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

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top