0

I'm trying to create my first game in Java, but I keep getting an IllegalAccesError. Both files are saved insinde the same folder. Everything is set to public and the error only occurs at runtime, so tha would mean that the class should've incompatibly changed, but I can't find it. Can someone help me out? It's probably an obvious mistake but I'm a total noob. Thanks in advance!

Exception in thread "main" java.lang.IllegalAccessError: class Game tried to access field Hero.Level (Game is in unnamed module of loader com.sun.tools.javac.launcher.Main$MemoryClassLoader @76b10754; Hero is in unnamed module of loader 'app') at Game.main(Game.java:9)

public class Game {

  public static void main(String[] args){
    Hero Tim = new Hero();
    Tim.nameChar("Tim");
    System.out.println("Dit is " + Tim.Level);
  }
}

public class Hero{
  String Character;
  int Hitpoints;
  int Attack;
  int Experience;
  int Potions;
  int Level;

  public Hero(){
    Character = "Unnamed";
    Hitpoints = 100;
    Attack = 10;
    Experience = 0;
    Potions = 1;
    Level = 1;
  }

  public void nameChar(String inputName){
    this.Character = inputName;
  }

  public void checkLVL(){
    if(Experience >= 100){
      Level += 1;
    }
  }
}
  • i ran Game it ran without any access issue – sanjeevRm Mar 31 '21 at 09:31
  • Thanks, I managed to solve it. I ran the fles from the windows command prompt which didn't work, so I downloaded an extension for my code editor which has its own java compiler and there it worked flawlessly. I think it might be an issue with windows storing away data in strange places. Anyways, thanks for the answer, it triggered me to try some other methods of executing the code :) – Tim Groothuis Mar 31 '21 at 09:46
  • 1
    "Everything is set to public" - no it's not. *None* of your fields are public. They're package-private. (I would strongly suggest making them *actually* private, and renaming them to follow Java naming conventions, and provide property methods to access them.) – Jon Skeet Mar 31 '21 at 10:04

0 Answers0