Search code examples
javanetbeansnetbeans-8

NetBeans - How to import another java file?


So in my source packages, I have one package called DiceRoller. Within that, I have two java files, one is MainApp.java and the second is TwoDice.java.

The MainApp is going to be the one I will be running. TwoDice.java will have the coding that will roll a pair of dice. I then want MainApp.java to display what numbers have been rolled.

Basically, if I declare

`int output = 12;` 

in TwoDice.java, how would I be able to

system.out.println(output);

in MainApp.java and display 12 without having any errors?


Solution

  • If both classes are in same package then no need to import it. In TwoDice.java you would have a method like this...

      public int getOutput() {
          return output;
       } 
    

    In you MainApp.java, you could then access it like this...

       private static void main(String[] args) {
            TwoDice dice = new TwoDice();
            System.out.println(dice.getOutput();
        }