0

I have classes called Logger and Error Handler in separate projects. These are each made into jar files which I can then use as libraries in various other projects.

I now need to use the static functions from Logger class in the Error Handler class. I also need to use the same functions from Logger class and some static functions from Error Handler class in class C which is in a separate project.

Right now I import the Logger jar inside Error Handler in order to use said functions and then export this entire thing as a jar.

And then in my class C I import the Error Handler Jar as well as the Logger jar.

So if I try to use any function from Logger, I get 2 import suggestions, 1 from the Error Handler jar and 1 from the Logger jar.

I want to avoid this as I would have to remake the Error Handler jar everytime there is a change in the Logger functions..

I hope that explains things better.

  • 1
    You need to import the file in both places. Why don't you want to do this? – NomadMaker Feb 10 '21 at 07:24
  • I would end up importing the jar in 2 places.. say I imported B inside A and then A inside C.. I would have 2 of the same class, to avoid redundancy and confusion I would Like to import only in one place – bobbing_developer Feb 10 '21 at 07:51
  • You seem to misunderstand what _importing_ is, which is merely a way to refer to a class (or member, with static) by its short name. You don't "import" jars, and importing doesn't create extra copies of a class. – chrylis -cautiouslyoptimistic- Feb 10 '21 at 08:08
  • That would be a case of transitive dependency. If project A imports project B and project C imports project A, then B is a transitive dependency. Even if you import project B explicitly in project C, there won't be any problems. – Aniket Sahrawat Feb 10 '21 at 08:21
  • That isn't how java works. You need to import the file into each file that needs it. This doesnt create problems in java. – NomadMaker Feb 10 '21 at 08:31
  • @AniketSahrawat I know there wont be any problems. I merely wanted to avoid importing the jars in multiple places.. I don't think I explained my problem well. But I asked a few colleagues and realised that what i was trying to achieve is not possible. Thanks – bobbing_developer Feb 10 '21 at 09:36
  • Given the above problem statement, this is not impossible but a clear case of misunderstanding. If you think you were unable to explain the problem, add more information by [Edit](https://stackoverflow.com/posts/66132461/edit)ing the question. – Aniket Sahrawat Feb 10 '21 at 09:39
  • @AniketSahrawat I edited the question. Hope that explains things better – bobbing_developer Feb 10 '21 at 09:59
  • Having logger and error handler in separated jars makes absolutely no sense to me. These should be inside the project itself. I also do not understand how multiple suggestions can appear even if you import a jar multiple times. Maybe your IDE has a bug or some configuration is messed up. – Aniket Sahrawat Feb 10 '21 at 10:13
  • The class names were just an example. – bobbing_developer Feb 10 '21 at 10:17

1 Answers1

1

A class doesn't need to be imported by another to be used. You can refer to it by its fully qualified name; e.g.

public static void main(String[] args)  {
    java.util.ArrayList l = new java.util.ArrayList();
}

However it is bad style to do that (unless it is absolutely necessary) because it make your code hard to read.

It is also possible for one class to refer to another without an import if they are both declared in the same package.


On the other hand, you may be asking if you can refer to one class from another class without adding it as a dependency; i.e. without its .class file being present on the classpath at compile time.

That's not possible. (Unless you are using reflection ... which is a lot more work than adding import statements.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216