Friday 14 September 2018

Static and Dynamic class loading

Static class loading
Classes are loaded using new operator. Whenever you define new object using the new keyword, then java class loader fetches and load the corresponding class, if that class is not loaded already.

Dynamic class loading
Dynamic class loading allows you to load the classes at run time. It is useful to load the class, when the name of class is not known at compile time.

Let me explain with an example, suppose you are a toy vendor, where you sell different toys to the customers. When you launch your application, instead of loading all the toy classes, load the class whenever customer asks for the toy first time. This way you can reduce the memory foot print.

Find the below working application.

ToyInformation.java
package com.sample.toys;

public interface ToyInformation {
 String toyInformation();
}

Car.java

package com.sample.toys;

public class Car implements ToyInformation{

 @Override
 public String toyInformation() {
  return "I am a car";
 }

}

Lion.java

package com.sample.toys;

public class Lion implements ToyInformation{

 @Override
 public String toyInformation() {
  return "I am a lion";
 }

}

Train.java

package com.sample.toys;

public class Train implements ToyInformation {

 @Override
 public String toyInformation() {
  return "I am a train";
 }

}

Application.java

package com.sample.app;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import com.sample.toys.Car;
import com.sample.toys.Lion;
import com.sample.toys.ToyInformation;
import com.sample.toys.Train;

public class Application {

 private static Map<Integer, ToyInformation> toysMap = new HashMap<>();

 private static void printToyNames() {
  System.out.println("*********************");
  System.out.println("1. Car");
  System.out.println("2. Lion");
  System.out.println("3. Train");
  System.out.println("*********************");
  System.out.println("Select the toy that you want");
 }

 private static void getToyInformation() throws Exception {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  int selected = -1;

  try {
   selected = Integer.parseInt(br.readLine());
  } catch (NumberFormatException e) {
   System.out.println("Not a valid selection");
   return;
  }

  switch (selected) {
  case 1:
   if (!toysMap.containsKey(1)) {
    Car car = (Car) Class.forName("com.sample.toys.Car").newInstance();
    toysMap.put(1, car);
   }
   break;
  case 2:
   if (!toysMap.containsKey(2)) {
    Lion lion = (Lion) Class.forName("com.sample.toys.Lion").newInstance();
    toysMap.put(2, lion);
   }
   break;

  case 3:
   if (!toysMap.containsKey(3)) {
    Train train = (Train) Class.forName("com.sample.toys.Train").newInstance();
    toysMap.put(3, train);
   }
   break;
  default:
   System.out.println("Sorry...We do not have the copy");
  }

  ToyInformation toyInfo = toysMap.get(selected);

  if (toyInfo != null) {
   System.out.println(toyInfo.toyInformation());
  }
 }

 public static void main(String args[]) throws Exception {
  while (true) {
   printToyNames();
   getToyInformation();
  }
 }
}


Output



You may like

No comments:

Post a Comment