I’m not new to Java but I never learned Packages. Anyways, let’s say I create a folder called maina.
I put in file main.java in folder maina:
package maina;
import other.Dice;
public class main
{
public static void main(String[] args)
{
System.out.println("Hello world!");
System.out.println(Dice.test);
}
}
Then I create a new folder called other inside the folder maina. In folder other I put file Dice.java:
package other;
public class Dice
{
public Dice() {
String test = "Testing!";
}
}
OK, now Dice.java compiles fine.
However when I compile main.java I get this error:
C:\Users\tekaffi\Documents\ask\maina\main.java:13: cannot find symbol
symbol : variable test
location: class other.Dice
System.out.println(Dice.test);
^
1 error
Tool completed with exit code 1
What am I doing wrong?
Here’s the error I get when I compile:
C:\Users\wekaffi\Documents\ask\maina\myMain.java:3: package maina.other does not exist
import maina.other.Dice;
^
C:\Users\wekaffi\Documents\ask\maina\myMain.java:13: cannot find symbol
symbol : class Dice
location: class maina.myMain
Dice myDice = new Dice();
^
C:\Users\wekaffi\Documents\ask\maina\myMain.java:13: cannot find symbol
symbol : class Dice
location: class maina.myMain
Dice myDice = new Dice();
^
3 errors
Tool completed with exit code 1
It has nothing to do with packages.
Your code is seriously messed up, you’re trying to call a “test” member on the “Dice” class but you haven’t created that member. besides that, you can’t have a class named “main” and then have a static main method in it beacuse the compiler will think the main method is the constructor you need to rename your class to something else.
For your code to work your Dice class needs to look like this:
And for the print to work you need to create a new instance of Dice before you print
Either that or you make Dice static. So your main needs to be renamed to myMain and then the class should look like this:
If you’re placing stuff where you said you are, it should work fine package-wise