Solved How to call a method from another class? >.<

Discussion in 'Plugin Development' started by Geekhellmc, Jul 14, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Geekhellmc

    I know this soud dump but I forgot how to call a method from another class to onEnable >.<
     
  2. Offline

    TheMcScavenger

    Is it a static method?
    Code:java
    1. <Classname>.<Methodname>();

    Otherwise:
    Code:java
    1. <Classname> name = new <Classname>();
    2. name.<Methodname>();
     
  3. Geekhellmc
    Here's a couple different ways
    • Create a new instance of it
    • Use static
    • Use reflection
    Creating a new instance:
    Code:
    SomeClass someClass = new SomeClass();
    someClass.someMethod();
    Using static:
    Code:
    SomeClass.someMethod(); // someMethod must be static
    Using reflection:
    Code:
    Class<?> someClass = Class.forName("SomeClass");
    Method someMethod = someClass.getDeclaredMethod("someMethod", parameterTypes);
    someMethod.invoke(objectToInvokeOn, parameters);
     
  4. Offline

    Rocoty

    It's no good looking for a good answer to your question here. It touches the basics of Java, and the answers you have gotten here tell you only half the story. You need to know when to use which solution, which implies a good understanding of Java and especially OOP.
     
  5. Assist You also shouldn't be teaching beginner's about reflection, other than to imply it doesn't exist or that it's evil. It's for more advanced users and something can easily go wrong, especially in the hands of a beginner.
     
  6. Offline

    rfsantos1996

    //Offtopic LOL, nice signature xD
     
    AdamQpzm likes this.
  7. Offline

    Geekhellmc

    I found the answer thank you. AdamQpzm I am not a beginner. I have done plugins before for my server.
     
  8. Offline

    Gater12

    Geekhellmc
    But you don't know how to "access methods from another class" prior?
     
  9. Offline

    Geekhellmc

  10. AdamQpzm
    I know. I thought I'd throw it in anyways as it is a way to do what he asked, but I am also not expecting him to use it.

    Geekhellmc
    This is a part of the very basics of Java, you must not be very experienced if you forget something like this.
     
    AdamQpzm likes this.
  11. Offline

    Geekhellmc

    Assist I didn't use your code, I read and understood it. I made my own way
     
Thread Status:
Not open for further replies.

Share This Page