3

I'm trying to create a Java API that I will use in other projects.

I understand that if I create new classes I can make objects in the other projects that have those classes. But what I want right now is a way to just create a method that isn't part of an objects that I can call upon whenever I wish without having to create a new object.

So what I'm asking is, how would I create a method like this where I can pass in parameters from another project and the api project will do whatever I put in the method involving those perameters? Do I need to make the methods static or what?

When I try to just access the class with the class name and then import that class from the api, it doesn't allow me to use a public void method that I created.

2

Yes, a static method is what you will need for this. An example of a static method is java.lang.Thread.currentThread().

However, be advised that static methods are usually a bad idea, and best avoided. Also, be advised that it is especially people who are new to programming that are prone to make bad use of static methods. So, if you are new to programming, you are most likely a prime example of someone who is about to make bad use of static methods.

I would recommend that you sit down and think hard how you could make this a regular ("instance") method of a normal object. Your "other projects" would create an instance of that object, and invoke its methods.

If you absolutely have to use static methods, you should still minimize them or hide them.

To minimize static methods, consider the singleton pattern: declare just one static method called getTheOneAndOnlyInstanceOfMyObject() and make it return an object containing non-static ("instance") methods that can be invoked by your "other projects".

To hide static methods, create a facade object which your "other projects" can instantiate and invoke, and then have this facade object internally invoke the static methods.

Edit:

For a discussion of why static methods can be (or are potentially) bad, see this answer: https://stackoverflow.com/a/752805/773113

  • This is all excellent advice for any new programmer. My humble suggestion would be to explain WHY one should avoid using static methods. Consider explaining multi-threading concerns and difficulty in mocking/testing that come along with static methods in order to give a more comprehensive picture. – Nathan Oct 5 '15 at 21:30
  • 1
    @Nathan true, that would have made my answer more complete, but also more off-topic. So, I decided not to repeat things that have already been eloquently said a thousand times over, and I amended my answer to include a reference to a relevant discussion instead. – Mike Nakis Oct 5 '15 at 22:49
  • The only difference between singletons and static methods is that singletons can have constructors that are only called if the object is being used. – Idan Arye Oct 6 '15 at 9:06
2

Don't use static method if it contains any global state. It's bad design. But public static methods is good for "utility" classes/methods with no state between method calls. java.lang.Thread.currentThread() is not a good example. Collections.sort() is much better. Disadvantages as were mentioned:

  1. It's difficult to mock them in unit tests. But usually you don't. It contains some 'algorithm' and does not contain any state (no external dependencies).
  2. You can't override static method by subclassing. This can be a case. So consider these two options in your API. Don't avoid static methods just because Java is object-oriented language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.