Solved Passing a method from a class to another class

Discussion in 'Spigot Plugin Development' started by dino14, Jun 30, 2020.

  1. Hello,

    I want to pass some methods from a class without using singleton as I will be using multiple objects...

    I created a class named Spawns:
    Code (Java):
    public class Spawns {
        private double x,y,z;
        private String world;
        private static int nr;
        public Spawns(){
            x = 0.0;
            y = 0.0;
            z = 0.0;
            String world = "world";
        }
        public void setWorldSpawn(double x, double y, double z, String world){
            this.x = x;
            this.y = y;
            this.z = z;
            this.world = world;
        }

        public static int getNr(){
            return nr;
        }
    In the other class, I've created a new object of type Spawns:
    Code (Java):
    Spawns test = new Spawns();

    test.setWorldSpawn(x,y,z,"world");
    This is not working... it says: Cannot resolve symbol 'setWorldSpawn' Shouldn't this code work ?
    I used to use getters and setters in c++ just like this, create the object, then I just call the method with object.setNr(0);
    Also in the oracle documentation they are doing the same thing here: https://docs.oracle.com/javase/tutorial/java/javaOO/objects.html
    like this:
    Code (Text):
            Rectangle rectTwo = new Rectangle(50, 100);
           
            // set rectTwo's position
            rectTwo.origin = originOne;
     
  2. The problem is with this line:
    Code (Java):
    String world = "world";
    The same variable name exists in the class

    Just replace the Spawns() with this:
    Code (Java):
    public Spawns(){
          x = 0.0;
          y = 0.0;
          z = 0.0;
          world = "world";
    }
     
  3. Yeah, I replaced that, but still can't call the methods using "test" object.
     
  4. You are creating an instance of your Spawns class correctly if you have updated your code to do what @KingCobra suggested. The issue lies somewhere else, perhaps post the method you are using the object in?
     
  5. Oh, so I was trying to call those methods without being in another method...Sorry for bothering you. I created the object and called that method (from the Spawns class) in onCommand method and now works as expecting.