r/learnjava • • Dec 08 '17

Calling methods of a different class query

Hi guys.

Say I have a method in class A.

I then wish to call the method from class A in public static void main(String[] args).

How would I do so?

0 Upvotes

22 comments sorted by

View all comments

3

u/empire539 Dec 08 '17

If the method is non-static, then you probably will need to instantiate an A object first, e.g.

A a = new A();
a.myMethod();

Otherwise, if the method is static, you should be able to call it directly on A: A.myStaticMethod().

1

u/koproller Dec 08 '17

Can't you also make singleton?

4

u/that_one_dev Dec 09 '17

Do you mean something like this?

Singleton.getInstance().someMethod()

That would be fine but I don't think someone who doesn't understand objects and methods should be worried about a singleton just yet

-1

u/[deleted] Dec 08 '17

Oh, yeah forgot about that....thanks! :)

4

u/that_one_dev Dec 09 '17 edited Dec 09 '17

I don't think you know what a singleton means if you don't know how to call a method