In C/C++, the API for a library is declared in header files. I can look into header files to learn which functions and types the library provides. But Java doesn't use the concept of header files. What does it use instead? Where can I see the interface declaration for a Java library?
-
There are `import`s and an `interface` is a special thing in Java... – deHaar May 07 '19 at 14:24
-
2javadocs give the declaration, and one hopes intelligent information on the usage. – KevinO May 07 '19 at 14:25
3 Answers
Simply spoken: you can't.
There is no universal rule that applies to Java how exactly to do that. Sure, you can check out for actual interface
java classes, and you can search for public classes and their public methods. But there is no "central" place where such information must come together.
Nonetheless, with recent Java, you would define clear modules, and would write good Javadoc comments to describe your packages, and how to use them.
In other words: there are certain concepts that help java developers to describe "this is here is an interface meant for external consumption, and here is information how to use that interface". But how you do that exactly is your choice.
From that point of view, it is pretty similar to C++. It doesn't help you to have header files and cpp source files separated ... when the content within the header files isn't written in reasonable ways.
Finally: the real answer is probably: you rely on your IDE. When I want to find usages or implementations of an interface, I go ask my IDE, which knows my project and can show me such things.

- 137,827
- 25
- 176
- 248
In java you can use all public
classes defined in the jar file.
Inside the class you can call all the public methods.
THere are two other visibility levels that you can use, protected and default visibility (without any visibility keyword) that can be used with some restrictions.
Here a complete visibility table (copied from this other answer):
│ Class │ Package │ Subclass │ Subclass │ World
│ │ │(same pkg)│(diff pkg)│
────────────┼───────┼─────────┼──────────┼──────────┼────────
public │ + │ + │ + │ + │ +
────────────┼───────┼─────────┼──────────┼──────────┼────────
protected │ + │ + │ + │ + │ -
────────────┼───────┼─────────┼──────────┼──────────┼────────
<default> │ + │ + │ + │ - │ -
────────────┼───────┼─────────┼──────────┼──────────┼────────
private │ + │ - │ - │ - │ -
+ : accessible - : not accessible
Where this are the definitions of columns:
- Class - The same class (this is not your case because you are using classes defined by an external library)
- Package - Any class defined in the same package of the class that you need to access
- Subclass (same package) - Any class that is a subclass of the class that you need to access and defined in the same package
- Subclass (different package) - Any class that is a subclass of the class that you need to access and defined in a different package
- World - Any other class (not same package and not subclass)

- 137,073
- 23
- 153
- 219

- 26,420
- 4
- 39
- 56
-
jar file is just an archive of *.class files. Those files are binary. They can contain `public` classes and methods but I can't just read them because they are binary. – anton_rh May 07 '19 at 14:31
-
1@anton_rh You need to know what is defined inside the jar. You can access the javadoc files, if that is not present you can decompile the java classes inside the jar. Generally all the public libraries offers a public documentation. In any case getting the firm of each method is quite simple using a decompiler. – Davide Lorenzo MARINO May 07 '19 at 14:33
One way is that, by convention, Java developers publish Javadoc with their APIs. Sometimes in the same jar file, but usually in a separate one with a similar name downloaded from the same place.
If you don't have the Javadoc, you can find the public interface of a library through Reflection: think C++ RTTI on steroids, whereby every public class name, every public method name and its signatures, including the names of its arguments, are visible and can be retrieved programmatically at run time as well as at design time. An IDE is a good tool to help you with this; it uses reflection to tell you the available classes and shows you the signature of a method as soon as you open a parenthesis after its name.

- 5,963
- 3
- 21
- 42