java - Attempting to reference non-static field with non-static method results in error -


i have created class, obstacle, obstacle constructor , function.

package environment;  import static java.lang.system.out; import environment.worldenvironment;  public class obstacle {      private string obstacletype, setobstacletype;     private int obstaclesize, obstaclexcoord, obstacleycoord,   setobstaclesize, setobstaclexcoord, setobstacleycoord;  public obstacle(string gettype, int getsize, int getxcoord, int getycoord){      obstacletype = gettype;     obstaclesize = getsize;     obstaclexcoord = getxcoord;     obstacleycoord = getycoord;  }  public void generateobstacle(int getplayercurrentxcoord, int   getplayercurrentycoord){//code in generateobstacle} 

in main method, call generateobstacle() different class (note both class , obstacle class within same package).

import environment.obstacle;  public void main(string[] args){      spawnplayer();      while(rungame){          //one example of calling obstacle member variables , calling generateobstacle()         switch((playerinput.nextline()).touppercase()){              case "w":                  if(currentxcoord + 1 == obstacle.obstaclexcoord && obstacle.obstacleycoord == currentycoord){                      out.println("there " + (obstacle.obstacletype).tolowercase() + " trying go.");                     printplayercoords();                  }                 else{                      moveplayerforward();                     printplayercoords();                     if(spawnobstacle.nextint(8) == 3){                          obstacle.generateobstacle(currentxcoord, currentycoord);                      }                  }                 break; 

all

obstacle. 

variable references throw error, saying "cannot make static reference non-static field. same with

obstacle.generateobstacle(currentxcoord, currentycoord); 

i'm not sure why i'm being given error, because no member variables of obstacle static, , main method not static. there no static fields in generateobstacle() method itself. obstacle constructer:

obstacle newobstacle = new obstacle(setobstacletype, setobstaclesize, setobstaclexcoord, setobstacleycoord); 

is there can explain me why i'm getting error? , yes, have looked around have not found explanatory solutions. in advance.

you can not call generateobstacle() method -

 obstacle.generateobstacle(currentxcoord, currentycoord); 

since generateobstacle() non-static method in class.

solution:
(1) make generateobstacle() method static , call class name-

obstacle.generateobstacle(currentxcoord, currentycoord);

(2) or create instance of obstacle, anobstacle use call method -

 obstacle anobstacle = new obstacle(...);  anobstacle.generateobstacle(currentxcoord, currentycoord);   

note: non-static type member of class (that method or variables) lives within object of class. means non-static members usable when object created. not true static member of class. can access static member of class without creating instance/object of class.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -