non static error message need help thanx guys

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boomba
    New Member
    • Apr 2008
    • 5

    non static error message need help thanx guys

    hi there guys
    im currently doing an assessment and have found this non static error which im stuck at here is my code for the class i know its ugly lol but the teacher started us off lol

    public class Player
    {
    private String name;
    private World world;
    private int unplacedArmies;
    private int placedArmies;
    private int turn;

    public Player(String name, World world)
    {
    placedArmies();
    unplacedArmies( );
    startTurn();
    name();
    }

    /**
    * Return the number of territories that this player owns.
    */
    (ignore this bit ) public int territoryCount( )
    {
    // You should not modify this method.
    //
    // Note: This code is very ugly because we haven't learnt about
    // arrays and loops yet.
    //
    // You should NEVER write code like this! ;-)
    //
    int count = 0;
    if (world.africa() .territory1().o wner() == this) count++;
    if (world.africa() .territory2().o wner() == this) count++;
    if (world.africa() .territory3().o wner() == this) count++;
    if (world.africa() .territory4().o wner() == this) count++;
    if (world.euroAsia ().territory1() .owner() == this) count++;
    if (world.euroAsia ().territory2() .owner() == this) count++;
    if (world.euroAsia ().territory3() .owner() == this) count++;
    if (world.euroAsia ().territory4() .owner() == this) count++;
    if (world.australi a().territory1( ).owner() == this) count++;
    if (world.australi a().territory2( ).owner() == this) count++;
    if (world.australi a().territory3( ).owner() == this) count++;
    if (world.australi a().territory4( ).owner() == this) count++;
    if (world.northAme rica().territor y1().owner() == this) count++;
    if (world.northAme rica().territor y2().owner() == this) count++;
    if (world.northAme rica().territor y3().owner() == this) count++;
    if (world.northAme rica().territor y4().owner() == this) count++;
    if (world.southAme rica().territor y1().owner() == this) count++;
    if (world.southAme rica().territor y2().owner() == this) count++;
    if (world.southAme rica().territor y3().owner() == this) count++;
    if (world.southAme rica().territor y4().owner() == this) count++;

    return count;
    }




    public void startTurn()
    {
    turn++;
    unplacedArmies = (territoryCount () /3);
    System.out.prin tln(name + "'s turn #" + turn++);
    System.out.prin tln("you have" + placedArmies + "armies placed.");
    System.out.prin tln("you have" + unplacedArmies + "armies to be placed.");
    }

    (this is where the error is)
    public void placeArmy(Terri tory territory)
    {
    placedArmies = placedArmies + 1;
    unplacedArmies = unplacedArmies - 1;
    Territory.armie s() = Territory.armie s() + 1; }

    the last line is where i get the message saying "non static error armies() cannot be referenced from a static context

    public void attack(Territor y myTerritory, Territory enemyTerritory)
    {
    }

    public String name()
    {
    return name;
    }

    public int placedArmies()
    {
    return placedArmies = 0;
    }

    public int unplacedArmies( )
    {
    return unplacedArmies = 6;
    }
    }
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Assuming that your Territory class has a method called armies(), you can't call it that way unless it's declared with the static keyword (don't do that unless you know what it means). Instead, create an instance and then call the method.

    [CODE=java]
    Territory t = new Territory();
    t.armies();
    [/CODE]

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Also realize that in that method Territory is the class and territory with a small 't' is the reference. Java is case-sensitive.

      Comment

      Working...