By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,736 Members | 1,982 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,736 IT Pros & Developers. It's quick & easy.

When are classes loaded by other classes?

P: n/a
If I have a class like this:

public class Tester {

ObjectType1 ot1;
ObjectType2 ot2;
ObjectType3 ot3;

public void main(String args) {
//Preliminary checking, resulting in bRun being
//true or false -- true means we continue, false, we quit:
if (bRun) {
Tester t = new Tester();
t.doStuff();
}
System.exit(0);
}

public Tester() {
//do stuff to create an instance of this class
ot1= new ObjectType1;
ot2= new ObjectType2;
ot3= new ObjectType3;
return;
}

public void do Stuff() {
//main processing routines to do stuff in this object
return;
}
}

Does this class load the files for the classes ObjectType1, 2, and 3 when
this class is first called from the command line, or does it wait and load
those classes from disk when the main() method calls Tester() to create an
object? In other words, if bRun is false, and the main() method never
creates an object of type Tester, are the other classes ever loaded?

I'm concerned about speed and trying to create a small class to determine
whether another class, that uses a good number of objects (like
ObjectType1, 2, & 3 are used above) needs to run. I won't have to create
an extra class if I can keep a long list of objects from loading until the
main() method actually creates the object.

Thanks!

Hal
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hal Vaughan <ha*@thresholddigital.com> wrote:
Does this class load the files for the classes ObjectType1, 2, and 3 when
this class is first called from the command line, or does it wait and load
those classes from disk when the main() method calls Tester() to create an
object?
See section 12.4.1 of the Java 2 Language Specification
http://java.sun.com/docs/books/jls/s...doc.html#57946

The executive summary is that Java requires "lazy loading": a class
must never be loaded until the JVM absolutely needs it.
I'm concerned about speed


Class-loading is a one-time thing; a class is only loaded once per JVM
execution (unless you fool around with classloaders). It's pretty hard
to get any leverage out of optimizing a one-time thing.

===

Now for my occasional reminder to everyone that comp.lang.java is not
an "official" newsgroup, and that you'll probably get a lot better
response using one of the worldwide Java newsgroups.

comp.lang.java was retired in 1996, replaced by a number of more
specific newsgroups. Most people don't see postings on comp.lang.java,
because only a few news servers still carry it. Google Groups carries
it for its historical value, and a few ISPs (mainly U.S.
cable-Internet providers) seem to carry it out of ignorance :-)

The current worldwide Java newsgroups are:

comp.lang.java.3d 3D Graphics API's for the Java language
comp.lang.java.advocacy Support for and criticism of the Java System
comp.lang.java.announce Announcements re the Java System (Moderated)
comp.lang.java.beans Java software components (JavaBeans)
comp.lang.java.corba Topics relating to Java and CORBA
comp.lang.java.databases Databases, java.sql, JDBC, ODBC
comp.lang.java.gui GUI toolkits and windowing: AWT, IFC etc
comp.lang.java.help Set-up problems, catch-all first aid
comp.lang.java.machine JVM, native methods, hardware
comp.lang.java.programmer Programming in the Java language
comp.lang.java.security Security issues raised by Java
comp.lang.java.softwaretools IDEs, browsers, compilers, other tools.
Jul 17 '05 #2

P: n/a
Doug Pardee wrote:
Hal Vaughan <ha*@thresholddigital.com> wrote:
Does this class load the files for the classes ObjectType1, 2, and 3 when
this class is first called from the command line, or does it wait and
load those classes from disk when the main() method calls Tester() to
create an object?
See section 12.4.1 of the Java 2 Language Specification

http://java.sun.com/docs/books/jls/s...doc.html#57946
The executive summary is that Java requires "lazy loading": a class
must never be loaded until the JVM absolutely needs it.


Actually, this helps a LOT. I want to be able to have a class run, check
ONE setting in a one line file and, if it's true, then run. If it's false,
don't run. If it runs, it'll start an entire system, with something like
20-30 classes. I wanted to be able to check the one setting and exit
quickly if the system was not supposed to run. If the JVM were to load all
the classes as soon as it was aware of them, then I'd have to do something
different to make a small class that would perform a "fast check".

Thanks!

Hal
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.