3

Does the Java Interpreter support an Autoloader-Function or a possibility to extend his "Looking for a class"-function?

In PHP there is a function called spl_autoload_register (see http://www.php.net/manual/en/function.spl-autoload-register.php for details ), which forces the interpreter to execute my own callback function when he is looking for a missing class. In my callback function I can include the file with the expected class.

The background-idea is that I'd like to switch dynamically between software-layers.

For example:
If class x_L2 exists in layer 2 then class x_L1 in the underlying layer 1 should be ignored. Actually I solved this issue with a general mirror class x, which have to inherit from the class in the highest available layer. But this way is error-prone when you have to maintain hundreds of classes.

Thanks in advance.

Snowfox
  • 1,112
  • 1
  • 10
  • 12

4 Answers4

1

This can be done by using a different class loader such as URLClassLoader. Then, you would get your classes from this class loader with loadClass. Yes, it also supports local and remote resources.

belgther
  • 2,544
  • 17
  • 15
1

You can write your own ClassLoader subclass, and use its loadClass() instead of the default one.

Viruzzo
  • 3,025
  • 13
  • 13
1

It seems you are trying to write a custom class loader ?

IBM has good documentation on that to help you on that.

Justin T.
  • 3,643
  • 1
  • 22
  • 43
0

Classes are identified by their package, as classes can be identified by namespaces in PHP. In order to use a specific class, you can simply use a qualified name.

public mypackage.mysubpackage.Myclass myInstanceofMyClass;

Or you could import only the needed class :

import mypackage.mysubpackage.Myclass;

You cannot have a class having the same name as another class in the same package. That's a duplicate définition of a type. I am not aware of a callback function for inclusion.

i.am.michiel
  • 10,281
  • 7
  • 50
  • 86