Java - Package woes
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 : clas开发者_如何学Gos 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:
package maina.other;
public class Dice
{
public String test;
public Dice() {
this.test = "Testing!";
}
}
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:
package maina;
import maina.other.Dice;
public class myMain
{
public static void main(String[] args)
{
System.out.println("Hello world!");
Dice myDice = new Dice();
System.out.println(myDice.test);
}
}
If you're placing stuff where you said you are, it should work fine package-wise
Your Dice class must have a package declaration like so
package maina.other;
Your main class should import Dice like so
import maina.other.*;
It'd be package maina.other, if it is in /maina/other
Dice
needs a public static string test
. The current test is a non-static local variable to the constructor. Or you can make test
non-static and then have the constructor set the test
member and then do new Dice().test
in the main.java
And the package name doesn't matter, foldering is only a convention and is IIRC ignored by the compiler. So thats not the issue here!
When you put package 'other' inside of 'maina' the new package is
package maina.other;
package maina.other;
As a side note, if you're using an IDE like Eclipse, you don't have to make the directories manually - it does it for you. Also navigating your packages with the package viewer is easy.
This one is tough to explain, and you've mentioned that you're new to Java, so please don't let me confuse you.
The package of a top-level type, such as main
or Dice
is whatever is listed in the package declaration. The package for Dice
could just be other
, even though the corresponding directory is nested inside the directory that corresponds to package amain
.
The key is resource discovery when compiling and running. When you compile, you can specify a sourcepath and a classpath that helps the compiler resolve dependencies. Likewise, when you run, you can specify a classpath that helps the JVM resolve dependencies. There is no restriction that root packages not be nested inside one another. So, both amain
and other
could be root packages, like so:
% cd <directory-that-contains-amain>
% javac -sourcepath .:amain amain/main.java amain/other/Dice.java
% java -classpath .:amain amain.main
This is considered abnormal (and consequently poor) practice, however. You shouldn't do it, but you could.
精彩评论