classloader.pdf - BFH/HTA Biel/DUE/Course 355/ Software...

This preview shows page 1 - 2 out of 4 pages.

BFH/HTA Biel/DUE/Course 355/Software Engineering 2Dynamic JavaClass Loaders1Class LoadersIntentClass loaders let Java dynamically load classes (and linking) on demand, at run-time. Applications may use predefined class loaders, or, if they must rely on spe-cific behavior, may construct their specific class loaders.Note.Class loaders are tightly coupled with security issues. Security, however,will not be discussed here, and is deferred to the course “Advanced Java Technol-ogies”.Load-Time Dynamic Loading and LinkingConsider the following program:public class Hello {public static void main(String[] args) {System.out.println("Hello, world!");}}BFH/HTA Biel/DUE/Course 355/Software Engineering 2Dynamic JavaClass Loaders2When the JVM loads classHello, it notices that this class uses classesStringandSystem, and extends classObject. If the JVM has not yet loaded one ofthose classes by this point, the JVM must run off and load these ones before itcan finish loadingHello.Run-Time Dynamic Loading and LinkingRun-time dynamic loading and linking loads a class file and resolves its namesuntil run-time, when the name of the class has been determined:public class X implements Runnable {public void run() {System.out.println("Class X.");}}public class Y implements Runnable {public void run() {System.out.println("Class Y.");}}BFH/HTA Biel/DUE/Course 355/Software EngineeringDynamic JavaClass Loaderspublic classLoadAndExecute{public static void main(String[] args) { // w/o arg checkingClass cl =Class.forName(args[0]);Object o =cl.newInstance();Runnable r =(Runnable) o;// assuming a Runnabler.run();}}Remarks:Variableclholds a class object (an instance of classClass).Class.forName(String)loads and links a class file at the point of its use,given the fully qualified class name.MethodnewInstancecreates an instance of the class the correspondingclass object stands for.If you know the type of the object (hereRunnable) then you can downcast tothe known type and apply corresponding methods on it.If you don’t know the type then you can usereflectionon the class object toobtain more information about the supported methods, etc.

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 4 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
NoProfessor
Tags
Object Oriented Programming, Subroutine, load classes, Class Loaders, Custom Class Loaders

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture