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?
- 
                    1Make the methods static - http://stackoverflow.com/questions/3963983/how-and-where-to-use-static-modifier-in-java – Kiril Kirilov May 31 '12 at 07:00
 
3 Answers
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();
}
- 51,780
 - 5
 - 72
 - 96
 
- 
                    What i'm asking is, is it possible to be able to call them, without having any kind of declaration? Like using the "A.foo();" Is it possible to just call it like foo();? – Nathan Kreider May 31 '12 at 08:33
 - 
                    @Nathan: To just call `foo()` you will need to place it in the same class. – npinti May 31 '12 at 08:42
 - 
                    
 - 
                    @NathanKreider: What you are after seems after all possible. I have ammended my answer. – npinti May 31 '12 at 08:48
 
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.
- 397
 - 3
 - 14
 
- 1,451
 - 11
 - 19
 
- 
                    The prerequisite of this is declaring the desired class members `static`. – Péter Török May 31 '12 at 07:04
 
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();
- 327
 - 4
 - 14