GuruKulki
GuruKulki

Reputation: 26418

What are the scenarios in which case a new class will be loaded?

That is when you have any jar file attached to you your class path. then as far as i know any class from the jar is loaded for the first time when,

  1. when you create any object of that type. or
  2. If you are accessing any static member or method of that class. or
  3. when you load that by using Class.forName() method.

So are there any other ways of loading a class for the first time?

Upvotes: 2

Views: 75

Answers (3)

OscarRyz
OscarRyz

Reputation: 199215

This may be helpful:

5.3 Creation and Loading on the The JavaTM Virtual Machine Specification

Upvotes: 2

Bozho
Bozho

Reputation: 597106

Also, whenever ClassLoader.loadClass(className) is called.

Upvotes: 1

Uri
Uri

Reputation: 89749

I think this pretty much covers it, though of course, class loading is "transitive".

If you are loading a class that references X (e.g., as a return value, as an instantiation, etc.), then X will have to be loaded.

I'm not sure if the loader is allowed to delay the actual load until the reference is really needed.

Upvotes: 2

Related Questions