Search code examples
javafileinstance

Connecting 2 java files in visual studio


I am coding a text adventure game in java and I am attempting to connect 2 different files to run the same game to make it a bit more organized.

Before I connected the files, this is how my main file (HopeTrail) ran. I set it up like this because I need my method startGame to be non-static because it is attached to a series of non-static methods that allows the game to progress.

public class HopeTrail
{
   public static void main(String[] args)
   {
      HopeTrail game = new HopeTrail();
      //new instance of the class HopeTrail
      
      game.startGame();      
   }

   public void startGame()
   {
      System.out.println("The game starts");
      introduction(); 
      //introduction is a method in HopeTrail that begins the game
   }

   public void introduction()
   { ... }
}

This worked, but then I wanted to have the bulk of my code in a file called HopeIntro.

I quickly realized that the main file would not compile when I tried to do this:

public class HopeTrail
{
   HopeIntro intro = new HopeIntro();
   //this is connecting the HopeIntro file to my main file

   public static void main(String[] args)
   {
      HopeTrail game = new HopeTrail();
      //new instance of the main file HopeTrail
      
      game.startGame();      
   }

   public void startGame()
   {
      System.out.println("The game starts");
      intro.introduction();
      //introduction is now in the HopeIntro file and I am calling 
      // it with 'intro' object
   }
}

and this is what HopeIntro looks like

public class HopeIntro
{
   HopeTrail trail = new HopeTrail();
   //this is letting HopeIntro view HopeTrail's methods

   public static void main(String[] args)
   {
      //this is HopeIntro main method
   }

   public void introduction()
   { ... }
}

I understand that the issue is that I am making a new instance of HopeTrail(); in its own main method as well as in the HopeIntro class. However I don't know how to fix this while still letting my game run.

for example, this compiled (because the new instance was not declared in the main method), however it doesn't fix my issue.

public class HopeTrail
{
   HopeIntro intro = new HopeIntro();
   HopeTrail game = new HopeTrail();

   public static void main(String[] args)
   {
      //intro.introduction(); does not work because: 
      // "cannot make static reference to the non-static field intro
      //and
      //game.startGame(); does not work for the same reason
   }
}

(HopeIntro looks the same as it did in my last example)

Any thoughts would be greatly appreciated. Thanks for taking the time to read this.

tldr: I want to connect 2 java files and still use an instance of my main file to start the game.


Solution

  • Not every class needs a main() method, only the application entry point does. Remove the main() methods, which are not the initial entry point to your application. Also, you don't need to create new instances of the HopeTrail class while inside the HopeIntro, which was created from a previous HopeTrail class. If you do so you would have two different instances of the HopeTrail class with two different states, which you might not want (but this depends on your game).

    If you want to "connect" two classes or need "access" from one class in another class, you can use references when you call methods. Depending on what you want to do you can define the HopeIntro.introduction() method like this:

    public class HopeIntro
    {
        public void introduction(HopeTrail trail)
        {
            // work with "trail" here
            String playerName = trail.getPlayerName();
            trail.showMessage("Hello "+playerName);
            
            // ... 
        }
    }
    

    You can call this method like this:

    public class HopeTrail
    {
       HopeIntro intro = new HopeIntro();
       
       public void startGame()
       {
          System.out.println("The game starts");
          intro.introduction(this); // pass "this" as the reference
                                    // to the HopeTrail object you are currently in.
       }
    }