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.

2
  • 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. Commented May 15, 2018 at 6: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. Commented May 15, 2018 at 6:13

1 Answer 1

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.*;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.