Skip to content

ifnazar/example-java-class-loaders

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Java class loader example

To test loading a class into a different class loader we will use the class "Timer". This class increments and prints in the console the value of a static variable

private static Integer count = 0;

In Java a static variable is shared between all instances of the class (which were loaded by the same classloader)

Running a class several times, all instances in the same "java classLoader" (default)
  • All instances will share the same static variable

instance_1: 1 instance_2: 2 instance_3: 3 instance_1: 4 instance_2: 5 instance_3: 6 instance_1: 7 instance_2: 8 instance_3: 9 ...

Each instance in a new "java classLoader"
  • Classes loaded by different classLoader do not share static variables

instance_1: 1 instance_2: 1 instance_3: 1 instance_1: 2 instance_2: 2 instance_3: 2 instance_1: 3 instance_2: 3 instance_3: 3 ...

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages