0

I would like to print the static variable of another class but I cant for some reason. Here is the error when compiling:

System.out.println(Creature.MAX_Y);
                               ^
  symbol:   variable MAX_Y
  location: class Creature
1 error

Here is the code for the Creature class:

package naturalselection.creature;

import naturalselection.food.Food;
import naturalselection.vector.Vector;
import naturalselection.creature.Target;
import java.util.*;
public class Creature{
  public static short MAX_X = 150;
  public static short MAX_Y = 150;
}

and the main class:

import naturalselection.creature.Creature;
import naturalselection.food.Food;

class Main{
  public static void main(String[] args){
    Creature c1 = new Creature(1, 1);
    System.out.println(Creature.MAX_Y);
  }
}

Here is the directory structure (part of it):

main.java
naturalselection -
+-- creature
    +-- Creature.java
3
  • 4
    Maybe a namespace/package problem. Please edit your question to include the complete file contents of your .java files. Also show how you compile your files. Commented Jul 26, 2020 at 18:16
  • @Progman I added the packages, the .java file for the Creature.java file is 200 lines long... but I've included the .java file for main Commented Jul 26, 2020 at 18:19
  • Recompile the naturalselection.creature.Creature class. Also check if you have a different Creature class (or even a Creature.class file somewhere) somewhere which is used instead. You might want to delete all .class files and recompile everything. Commented Jul 26, 2020 at 18:21

1 Answer 1

1

You need to declare static variable in that class from where you are trying to get variable. public static String variableName = "Value";

Sign up to request clarification or add additional context in comments.

1 Comment

No. This is blatantly wrong. You can refer to constants in other classes, and it is a very common design pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.