-2

when I try to access the method of another class, it gives me an error that non-static method can't be accessed from static method but none of my methods are static.

    import java.util.ArrayList;
    public class creatureClassDriverRathtarsGame
   {
public static void main(String[] args)
{
   creatureClass player = new creatureClass("name", 14,new locationClass(0, 0, 0));
   ArrayList <locationClass> locationRathTars = new <locationClass> ArrayList(5);
   for(locationClass r: locationRathTars)
   {
       int randomRow = (Math.random() * ((locationClass.getMaxRow()) + 1));
       int randomCol = (Math.random() * ((locationClass.getMaxCol()) + 1));
       creatureClass rathtars = new creatureClass("rathtars",0, new locationClass(randomRow, randomCol, 0));
   }

and the acessor method that is being called is

   public int getMaxRow()
{
    return maxrow;
}
public int getMaxCol()
{
    return maxcol;
}
ARC
  • 1
  • 1
  • 2
  • 3
    Please post an actual [mcve]. As it stands, I'm assuming your class is called `locationClass` and you are in fact calling methods from it statically. – CollinD Feb 09 '17 at 23:04
  • 2
    Following basic java code conventions would have pointed out the problem. Class names must start with a capital letter. instance (object) of a class should have a camel-cased name. – Slava Feb 09 '17 at 23:07
  • 2
    You are calling locationClass.getMaxRow() when you want to be calling r.getMaxRow() – antlersoft Feb 09 '17 at 23:07

1 Answers1

0

at first you must have the basic knowledge for java classes and objects and the difference between static and non-static members

to call a method using class name it must be static

so your acessor method should look like the blew

public static int getMaxRow()
{
   return maxrow;
}
public static int getMaxCol()
{
   return maxcol;
}

hope this is useful

Anas
  • 688
  • 7
  • 24