Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
Java - Cannot Use Class From Same Directory

I'm trying to call the Game class (that resides in src/main/java/Game.java) from within the Main class (that resides in src/main/java/Main.java), but I'm getting this error:

Main.java:3: error: cannot find symbol Game hangman = new Game(); symbol: class Game; location: class Main::: Main.java:3: error: cannot find symbol Game hangman = new Game(); symbol: class Game; location: class Main::: 2 errors; error: compilation failed

Here is the code that is in the Main.java file:

public class Main {
  public static void main (String[] args) {
    Game hangman = new Game();
    hangman.initialize();
  }
}

Here is the code that is in the Game.java file:

public class Game {
  private String randomWord;
  private int maxGuesses;
  private int guessCount;

  public Game () {
    this.maxGuesses = 8;
    this.guessCount = 0;
  }

  public void incrementGuessCount () {
    this.guessCount++;
  }

  public String generateRandomWord () {
    String[] words = {"puppy", "pool", "avalanche"};
    String result = words[(int) Math.floor(Math.random() * words.length)];
    this.randomWord = result;
    return result;
  }

  public void initialize () {
    String word = this.generateRandomWord();
    System.out.println(word);
  }
}

I'm using WSL, and this is my current Java version.

openjdk 11.0.13 2021-10-19 OpenJDK Runtime Environment (build 11.0.13+8-Ubuntu-0ubuntu1.20.04) OpenJDK 64-Bit Server VM (build 11.0.13+8-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!