0

I have the following problem. I wrote a project that contains a class and methods in it. I have exported the project to the jar because I want to use it as a library in another project. Is it possible to call the method without the declaration of the object?

public class Client {

public static void init(String host) {
    init(host, 123);
}

public static void init(String host, int port) {
    ClientAgent clientAgent = new ClientAgent();
    clientAgent.connect(new InetSocketAddress(host, port));
} }

What do I have to do to call the init method from the library in this way:

init("1231",124)

instead of

Client.init("1231",124) or new Client.init("1231",124)

when I importing

import... Client; or import ...Client.init; called method init(..,..) doesn't work.

Gorgeo
  • 7
  • 5
  • Your question implies that you dont understand the difference between static and non-static methods in the first place. Your **init** method is already static, therefore you should **never** invoke it on an **object**. You invoke it on the `Client` **class**. And as the answer suggests, you can *import* static methods as well. – GhostCat May 15 '18 at 06:10
  • And please note: terminology really matters. This is not declaring objects, either. Dont invent your own language - read good books and use the terms you find there. Anything else will confuse you, and then anybody you talk to. – GhostCat May 15 '18 at 06:13

1 Answers1

1

Use import static like below:

import static your.package.Client.init; // if you want only init method available as the static import

If you want all static method needs to available then use:

import static your.package.Client.*;
Amit Bera
  • 7,075
  • 1
  • 19
  • 42