Results 1 to 3 of 3
  1. #1
    daniel0 is offline Member
    Join Date
    Jan 2015
    Posts
    5
    Rep Power
    0

    Default Cannot make a static reference to the non-static method

    Alright, I have two classes, this one


    public class Player {

    private String player;
    public String getPlayer() {
    return player;
    }
    private int strength;
    private int defense;
    private int magic;
    private int resistance;
    public int getDefense() {
    return defense;
    }

    public void setDefense(int defense) {
    this.defense = defense;
    }
    private int skill;
    private int luck;



    public int getStrength() {
    return strength;
    }

    public void setStrength(int strength) {
    this.strength = strength;
    }

    public int getMagic() {
    return magic;
    }

    public void setMagic(int magic) {
    this.magic = magic;
    }

    public int getResistance() {
    return resistance;
    }

    public void setResistance(int resistance) {
    this.resistance = resistance;
    }

    public int getSkill() {
    return skill;
    }

    public void setSkill(int skill) {
    this.skill = skill;
    }

    public int getLuck() {
    return luck;
    }

    public void setLuck(int luck) {
    this.luck = luck;
    }

    public void setPlayer(String player) {
    this.player = player;
    }

    public Player(String name, int int1, int int2, int int3, int int4, int int5, int int6) {
    this.player = name;
    this.strength = int1;
    this.defense = int2;
    this.magic = int3;
    this.resistance = int4;
    this.skill = int5;
    this.luck = int6;}
    @Override
    public String toString() {
    return player + "" + strength + " " + defense + magic + " " + resistance + " " + skill + " " + luck;



    }
    }

    And the main class:


    public class Main {
    public static void main(String[] args) {
    Player player = new Player (null);

    Player[] playerList = new Player[] {new Player("Player1", 3, 3, 2, 1, 1, 3),};

    System.out.println(Player.getPlayer());

    }
    }



    However, it says that under Player.getPlayer() that it 'Cannot make a static reference to the non-static method'. I have looked around, but all the answers I could find fixed it by putting something in front of it, but it has not been working for me.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Cannot make a static reference to the non-static method

    Please place your code between code tags. Read about it --> BB Code List - Java Programming Forum - Learn Java Programming


    Regarding your problem, you are trying to access an instance method (getPlayer()) in a static way (by using the class name). You must use an instance of the class to access the method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    daniel0 is offline Member
    Join Date
    Jan 2015
    Posts
    5
    Rep Power
    0

    Default Re: Cannot make a static reference to the non-static method

    Thank you, at first, while I was reading other documents, I thought the issue WAS that it was a static method, so I was hesitant to change it, and I will use bbcode correctly.

Similar Threads

  1. Replies: 8
    Last Post: 11-19-2014, 08:58 PM
  2. Replies: 5
    Last Post: 03-31-2013, 03:30 AM
  3. Cannot make a static reference to the non-static method
    By Reskaillev in forum New To Java
    Replies: 4
    Last Post: 08-02-2011, 12:42 AM
  4. Replies: 10
    Last Post: 11-14-2010, 07:06 AM
  5. Replies: 7
    Last Post: 09-24-2010, 05:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •