Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to use two java classes with same name and same package?

Tags:

java

Is it possible to import and use two different classes with the same name and package in java?

For example, let's say I have two classes named "com.foo.Bar" that are slightly different. I'd like to be able to use both, but I have a restriction (because of stupid reflective crap) that forces me to keep the names and packages the same.

Is there some feature of java that would allow me to import and isolate each of these classes?

To elaborate, I changed my avro schemas in ways that they shouldn't have ever been changed (oops!) and now I'd like to go back and change the old avro files that can't be read with my new schema into files that can be read by my new schema. Avro seems to force you to use a specific class and package name to load the files.

like image 647
guidoism Avatar asked Jul 29 '11 22:07

guidoism


People also ask

Can we have multiple Java classes with the same name in the same package?

A Java file contains only one public class with a particular name. If you create another class with same name it will be a duplicate class. Still if you try to create such class then the compiler will generate a compile time error.

Can we have two classes declared with same name in Java?

No, while defining multiple classes in a single Java file you need to make sure that only one class among them is public.

Can you have two classes in Java?

class files created will be equal to the number of classes in the code. We can create as many classes as we want but writing many classes in a single file is not recommended as it makes code difficult to read rather we can create a single file for every class.

Can we use 2 packages in Java?

You cannot put package declaration twice in the same class. The package statement must be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.


4 Answers

Yes there is. You would need to implement your own Classloader and play some games to be able to access both during runtime.

I'm sure this is possible, because I ran into a very hard to debug issue where someone had a weird Classloader in their product that was messing up loading libraries and providing 2 different versions of the same file from 2 different versions of the library.

However, this sounds like an INCREDIBLY bad idea. I'd go back and find a different way of fixing your issue. This will only bring you heartache in the long run. Heck, it probably already is, as you investigate class loaders.

EDIT: To be specific, you cannot "import" both. But you can access both at runtime.

like image 153
rfeak Avatar answered Sep 27 '22 21:09

rfeak


No, java packages are used precisely to avoid that problem.

like image 42
João Silva Avatar answered Sep 27 '22 19:09

João Silva


If you really most definitely must do something like this, you can achieve it by using different classloaders and possibly reflection.

This is not the way Java works and it's not allowed on purpose - you shouldn't be doing stupid things which will screw up things for you.

like image 43
carlspring Avatar answered Sep 27 '22 21:09

carlspring


Yes it is. It does require you to make your own ClassLoader, though

I had made a demo of that on github to!

like image 39
brunoais Avatar answered Sep 27 '22 20:09

brunoais