anton_rh
anton_rh

Reputation: 9203

Where is the API for a library declared in Java?

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?

Upvotes: 0

Views: 158

Answers (3)

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

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)

Upvotes: 1

DodgyCodeException
DodgyCodeException

Reputation: 6123

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.

Upvotes: 1

GhostCat
GhostCat

Reputation: 140525

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.

Upvotes: 1

Related Questions