6

I have built a small (and 3 methods only!) api for myself, and I want to be able to call it like how you would call a method in Powerbot (A Runescape botting tool (I use it, but for programming purposes, not for actual cheating purposes)), without creating an Object of the file you'd require. How would i be able to do this?

Nathan Kreider
  • 516
  • 3
  • 7
  • 16

3 Answers3

7

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

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

And then, you can call them like so:

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

Note however that static methods need to be self contained.

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

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

And then do this:

import static sample.examples.Test.A;

public class Test2
{
    ...
    A();
}
npinti
  • 51,780
  • 5
  • 72
  • 96
3

If you use the static keyword when importing your class, you can use its methods as if they belong to the class you're importing them to. See:

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

And of course your "api methods" need to be static as well.

Rich Jahn
  • 397
  • 3
  • 14
tagtraeumer
  • 1,451
  • 11
  • 19
0

The best way i found out for me was to extend my activity (If i said it right)...

MAIN CLASS

public class myMainActivity extends myMiniApi{
...
}


I think this is a better way (my opinion) to do this, Just call your method like you normally would as if it were in the same class. example:

randomMethod();
AntonioSanchez
  • 327
  • 4
  • 14