javaobjectgetter

I'm having trouble using a getter method for an object that has been initialized, but is not in the same class as the getter method


I'm writing a text based game for my computer science class (I'm a beginner and still learning new stuff pretty much everyday) and I'm trying to make a method that will print out the information of the four party members; however, I ran into some trouble trying to make the getter method get the information about a specific player object that is in the main class. I'm trying to use the getter in a different class.

Here are the three classes in play with the related methods:

public class Main {
    
    public static void main(String[] args) {

        UIFunctions.startMenu();

    }//End of main method

    Characters smallMan = new Characters("Stuart Little", 2, 1.2, 0.45, false);
    Characters tallMan = new Characters("Big Mike", 6, 2.0, 0.30, false);
    Characters strongMan = new Characters("Barney Buff", 10, 1.5, 0.10, true);
    Characters deepPockets = new Characters("Guilo Baggin", 3, 1.6, 0.65, false);
    
}//End of main
public class Characters {

    private static String name;
    private static int strength;
    private static double height;
    private static double luck;
    private static boolean isGullible;

    public Characters() {}
    
    public Characters(String characterName, int characterStrength, double characterHeight, double characterLuck, boolean isStupid){
        name = characterName;
        strength = characterStrength;
        height = characterHeight;
        luck = characterLuck;
        isGullible = isStupid;
        
    }

    //--------------------------------------------------------
    // Getter methods
    //--------------------------------------------------------
    
    
    public static String getCharacterName() {
        return name;
    }

    public static int getCharacterStrength() {
        return strength;
    }
    
    public static double getCharacterHeight() {
        return height;
    }
    
    public static double getCharacterLuck() {
        return luck;
    }
}
public class UIFunctions{

public static void groupMenu(smallMan.getCharacterName) {
          
         
      }
}

I tried excluding the character name in front of the method declaration and just straight up putting the getter in the groupMenu method, but when I tested the program nothing happened, and it just finished.

I then tried the same thing but doing this instead:

public static void groupMenu(){

     smallMan.getCharacterName();
}

But that also failed.


Solution

  • You are trying to access the properties of your Characters objects (like smallMan) from a different class (UIFunctions). The issue you are facing is related to the scope and usage of your Characters objects and their properties.

    Change the Characters class to have instance variables (remove the static keyword from the fields) because you want each character to have its own set of attributes, not share them across all instances of the class.

    public class Characters {
        private String name;
        private int strength;
        private double height;
        private double luck;
        private boolean isGullible;
    
        public Characters() {}
    
        public Characters(String characterName, int characterStrength, double characterHeight, double characterLuck, boolean isStupid){
            name = characterName;
            strength = characterStrength;
            height = characterHeight;
            luck = characterLuck;
            isGullible = isStupid;
        }
    
        // Getter methods
        public String getCharacterName() {
            return name;
        }
    
        public int getCharacterStrength() {
            return strength;
        }
    
        public double getCharacterHeight() {
            return height;
        }
    
        public double getCharacterLuck() {
            return luck;
        }
    }
    

    Create instances of your Characters objects inside the Main class and make them accessible to other classes by declaring them as static if needed.

    public class Main {
        public static Characters smallMan = new Characters("Stuart Little", 2, 1.2, 0.45, false);
        public static Characters tallMan = new Characters("Big Mike", 6, 2.0, 0.30, false);
        public static Characters strongMan = new Characters("Barney Buff", 10, 1.5, 0.10, true);
        public static Characters deepPockets = new Characters("Guilo Baggin", 3, 1.6, 0.65, false);
    
        public static void main(String[] args) {
            UIFunctions.startMenu();
        }
    }
    

    In your UIFunctions class, you can now access the character information using the instance variables and getter methods like this:

    public class UIFunctions {
        public static void groupMenu() {
            System.out.println("Character name: " + Main.smallMan.getCharacterName());
            System.out.println("Character strength: " + Main.smallMan.getCharacterStrength());
            // Add similar lines for other character attributes
        }
    }