Avatar of msk_apk
Flag for India

asked on 

Java Class loading problem

I am trying to reload a class dynamically as per the below code.

1. After the first print, I am modifying the code and recompiling the code with different print statments. say for example the new printSomething method prints "here" alone.
2. after the second loadClass() statement, i am expecting the new printSomething() method to get executed which does not happen.  It always prints "printing here" irrespective of how many times I load the class through loadClass() method.

Believe I am missing something here.

import java.io.*;
import java.net.*;
public class DClassLoader {
	public static void main(String a[]) throws Exception
		ClassLoader sysLoader = DClassLoader.class.getClassLoader();
		Print l1 = (Print)sysLoader.loadClass("PrintObj").newInstance();

		Print l2 = (Print)sysLoader.loadClass("PrintObj").newInstance();

interface Print
	public void printSomething();

class PrintObj implements Print
	public void printSomething()
		System.out.println(" printing here");

Open in new window


Avatar of undefined
Last Comment

8/22/2022 - Mon