This site requires JavaScript, please enable it in your browser!
Greenfoot back
Mayokeks
Mayokeks wrote ...

2021/2/4

How to use a method of another class

Mayokeks Mayokeks

2021/2/4

#
I want to use a method I created in my first class also in the second class. Is there any possibility to use the method without creating the same one in the second class?
RcCookie RcCookie

2021/2/4

#
If it's a static method, you can simply say
1
ClassName.methodName();
If however, and that's what I am expecting, it's not a static method, you need to specifiy the instance of that class that you want to call that method on. For example, if you want to call the method on an actor that intersects this actor, you can use
1
2
ClassNamr object = (ClassName)getOneIntersectingObject(ClassName.class);
object.methodName();
Static methods are methods that are independent of all the variables of the instance of the class. You can make a method static by using the static keyword in front of the method name or variable name. But they can also just access the static variables of the class.
Mayokeks Mayokeks

2021/2/4

#
Thank you very much, finally I found the solution
You need to login to post a reply.