473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

When are classes loaded by other classes?

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
2 1983
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Craig Ringer | last post by:
Hi folks I'm a bit of a newbie here, though I've tried to appropriately research this issue before posting. I've found a lot of questions, a few answers that don't really answer quite what I'm...
6
by: Outshined | last post by:
I have a class library where just about every class uses its static initializer to register with a central registry object in the same assembly. I am hoping for some sort of Assembly.Load event...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
3
by: toki | last post by:
public class Form1 : System.Windows.Forms.Form { MyClass myclass; public Form1() { myclass = new MyClass(); } }
5
by: John Hardin | last post by:
All: Is it possible at runtime to determine the names of all classes derived from a given class? -- John Hardin KA7OHZ <johnh@aproposretail.com> Internal Systems...
6
by: sylcheung | last post by:
Hi, How can I be notified when the document load is complet in JavaScript? I am referring to the whold document load is complete, mean all images/external files/frame/iframes have been loaded. ...
3
by: ImOk | last post by:
In Smalltalk I can get the list of the classes, methods and global variables by querrying a global dictionary. In addition you can get the arguments. I know in PHP one can get the list of...
2
by: wzhao2000 | last post by:
Back in COM time, it's possible to use #import statement in cpp to load COM object type info and use it at coding time. When the application is started, the underlying COM DLL will not be loaded...
5
by: | last post by:
I am having problems with casting or converting a class to to an interface from which it derives. I'm certain that it's due to how it's being loaded, but I'm not sure how to get past the problem....
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.