473,749 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2001
Hal Vaughan <ha*@thresholdd igital.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*@thresholdd igital.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
3179
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 looking for, but nothing that really solves or explains all this. I'll admit to being stumped, hence my question here. I'm also trying to make this post as clear and detailed as possible. Unfortunately, that means it's come out like a book. I...
6
4821
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 that I can handle to go through my assembly and invoke the static initializer on each relevant class. This way, everything that needs to be registered will be registered before the first request to the registry object. So my questions are:
45
3614
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 themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
3
1423
by: toki | last post by:
public class Form1 : System.Windows.Forms.Form { MyClass myclass; public Form1() { myclass = new MyClass(); } }
5
2667
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 Administrator voice: (425) 672-1304 Apropos Retail Management Systems, Inc. fax: (425) 672-0192
6
2365
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. I think there is a window.addEventListener() function but I am not sure which event to listen to. Thank you.
3
2976
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 globals via the $GLOBALS array. But Is there a way to get the list of all the functions and classes/methods (and arguments) currently loaded in PHP by using reflection?
2
1853
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 until that part of code is called. Is there a similar thing in .Net (C#) application ? When I create C# application, I need to add reference to another assembly if I want to use its classes (I don't want to use reflection here). But this will...
5
1863
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. Here's a general outline of the architecture. It's oversimplified, but I think it's enough info to help: Assembly A.dll { interface IMyBase {}
0
8997
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8833
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.