OnJava8-Examples/reflection/SweetShop.java

43 lines
1009 B
Java
Raw Normal View History

// reflection/SweetShop.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// Examination of the way the class loader works
2015-06-15 17:47:35 -07:00
2015-12-15 11:47:04 -08:00
class Cookie {
static { System.out.println("Loading Cookie"); }
2015-06-15 17:47:35 -07:00
}
class Gum {
2015-11-03 12:00:44 -08:00
static { System.out.println("Loading Gum"); }
2015-06-15 17:47:35 -07:00
}
2015-12-15 11:47:04 -08:00
class Candy {
static { System.out.println("Loading Candy"); }
2015-06-15 17:47:35 -07:00
}
public class SweetShop {
public static void main(String[] args) {
2015-11-03 12:00:44 -08:00
System.out.println("inside main");
2015-06-15 17:47:35 -07:00
new Candy();
2015-11-03 12:00:44 -08:00
System.out.println("After creating Candy");
2015-06-15 17:47:35 -07:00
try {
Class.forName("Gum");
} catch(ClassNotFoundException e) {
2015-11-03 12:00:44 -08:00
System.out.println("Couldn't find Gum");
2015-06-15 17:47:35 -07:00
}
2015-11-03 12:00:44 -08:00
System.out.println("After Class.forName(\"Gum\")");
2015-06-15 17:47:35 -07:00
new Cookie();
2015-11-03 12:00:44 -08:00
System.out.println("After creating Cookie");
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
inside main
Loading Candy
After creating Candy
Loading Gum
After Class.forName("Gum")
Loading Cookie
After creating Cookie
2015-09-07 11:44:36 -06:00
*/