Call method without object

I built for myself (and 3 methods only!) An api for myself, and I want to be able to call it what you would call a method in Powerbot (a tool for launching Runescape bonuses (I use it, but for programming purposes, not for real goals of deception)) without creating the object of the desired file. How can I do this?

+4
source share
3 answers

If you use the static keyword when importing your class, you can use its methods as if they belonged to the class into which you are importing them. Cm:

http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html

And, of course, your "api methods" should also be static.

+2
source

You will need to create static methods, so you will need to do something like this:

public class A { public static void foo() { ... } } 

And then you can call them like this:

 public class B { ... A.foo(); } 

Note, however, that static methods must be self-contained.

EDIT: as recommended in one of the answers below, you can make it work like this:

 package samples.examples public class Test { public static void A() { ... } } 

And then do the following:

 import static sample.examples.Test.A; public class Test2 { ... A(); } 
+6
source

The best way that I learned for me was to extend my activity (if I said it correctly) ...

MAIN CLASS

 public class myMainActivity extends myMiniApi{ ... } 


I think this is the best way (my opinion) to do this, just name your method as usual, as if it were in the same class. Example:

 randomMethod(); 
0
source

Source: https://habr.com/ru/post/1415302/


All Articles