Junior — Middle
How can you implement a method call that exists in an external library?
sobes.tech AI
Answer from AI
To call a method from an external library in Java, you need to:
- Add the library to your project (for example, via Maven, Gradle, or manually adding a JAR file).
- Import the required class from the library.
- Create an object of that class (if the method is not static) or call the static method directly.
Example:
// Import class from external library
import com.example.lib.ExternalClass;
public class Main {
public static void main(String[] args) {
// Create an object
ExternalClass obj = new ExternalClass();
// Call the method
obj.externalMethod();
// Or call a static method
ExternalClass.staticMethod();
}
}
It is important to ensure that the library is correctly linked to the project, otherwise the compiler will not be able to find the classes.