Solved Basic Java Question

Discussion in 'Plugin Development' started by sara4012, Jun 15, 2014.

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

    sara4012

    First, I'd like to say don't say "Go look it up and don't annoy us!" I've searched the whole internet and cannot find it for the life of me!

    Now, I'd like to know how to run a method from another class. I have made a method in another class that gives a player a kit, but would like to call it from the main class. Thanks!
     
  2. Offline

    ArsenArsen

    Updater updater = new Updater(this, 81135, this.getFile(), Updater.UpdateType.NO_DOWNLOAD, false); Is for gravitys updater, what u need sara4012?
     
  3. Offline

    sara4012

    I need my main class to call a method from another class ArsenArsen
     
  4. Offline

    ArsenArsen

    I know how to run another class but not that sara4012 sorry.
     
  5. Offline

    Ice3ider4

    There are multiple ways to access methods from an other class but the easiest is to use the static modifier:

    You can add a static to the method like this:

    Code:java
    1. public static void yourFunction(parameters){
    2. }


    And with this you can access the method using the following code:
    Code:java
    1. YourClass.yourFunction(parameters);
     
  6. Offline

    Zupsub

    Make the method public+static.

    But java is OO (object oriented) so you shouldn't have that much static method.
    (Probably you don't heart anything about the keywords if written here, but the rest should be become clear for you with google and basic java knowledge!!
     
  7. Offline

    sara4012

    Ice3ider4 Thank you I never figured that out

    Zupsub I have experience just never figured this out.
     
  8. Offline

    AoH_Ruthless

    Zupsub Ice3ider4
    It is not wise to suggest the use of static modifiers to a new developer. This is an extremely bad practice and a very bad idea. Static should only be used when there are no other options or in classes like utilities, where the class does not need to be instantiated to work.

    sara4012
    You don't have to lie to us saying you have experience. Calling a method from another class is one of the first things I learned, and it is all over google if you search properly.

    However, their suggestions are rather bad (see my first paragraph).

    What you should do to call a method from another class:

    Let's say we have two classes, your main class and the class you want to access.

    Code:java
    1. public class MainClass {
    2.  
    3. public void method() {}
    4.  
    5. }



    Code:java
    1. public void SecondClass {
    2.  
    3. public void methodYouWant() {}
    4.  
    5. }


    In your main class, you need to pass a new instance. You don't have a constructor specified, so the default one has no parameters. To do this, we will make a globalized variable and instantiate it. In your main class, this is done in the onEnable(). In all other classes, you will need to run that in any method you want.

    Code:java
    1. public class MainClass {
    2. // Globalized variable
    3. private SecondClass sc;
    4.  
    5. public void onEnable() {
    6. // We are making a new instance of the class. If you don't do this, you will get NPE's when trying to use it.
    7. sc = new SecondClass();
    8. }
    9.  
    10. public void myMethod() {
    11. // Now you can call any method from the class SecondClass because you have instantiated it!
    12. sc.methodYouWant();
    13. }
    14.  
    15. // This is a getter method. Look it up.
    16. public SecondClass getSecondClass() {
    17. return sc;
    18. }
    19.  
    20. }
     
    Ice3ider4 likes this.
  9. Offline

    jthort

Thread Status:
Not open for further replies.

Share This Page