Reputation: 25
Following is the import statements in my code. The classes that am importing is in the same package comm
.
package comm;
import WikiLinksReducer;
import WikiPageLinksMapper;
import XmlInputFormat;
import RankCalculateMapper;
import RankCalculateReduce;
import RankingMapper;
Am getting the following errors when I compile the code:
WikiPageRanking.java:2: '.' expected
import WikiLinksReducer;
^
WikiPageRanking.java:2: ';' expected
import WikiLinksReducer;
^
WikiPageRanking.java:3: class, interface, or enum expected
import WikiPageLinksMapper;
^
WikiPageRanking.java:4: '.' expected
import XmlInputFormat;
^
WikiPageRanking.java:4: ';' expected
import XmlInputFormat;
^
WikiPageRanking.java:5: class, interface, or enum expected
import RankCalculateMapper;
^
WikiPageRanking.java:6: '.' expected
import RankCalculateReduce;
^
WikiPageRanking.java:6: ';' expected
import RankCalculateReduce;
^
WikiPageRanking.java:7: class, interface, or enum expected
import RankingMapper;
Upvotes: 1
Views: 2881
Reputation: 1322
I hope this can serve as a robust answer!
Java packages are meant to be a mechanism for managing naming and visibility of files.
Classes in a package will have "knowledge" of one another by default, and they will be unique from classes from external classes which appear to have the same name. The package name ensures that the naming convention is still unique. There are more nuances to Java packages, like limiting visibility in and between packages. Oracle has a decent tutorial for more specifics.
As for making a simple example, keep note of the following :
CLASSPATH
variable to include a path to your package directory.C:\java\mypackage
C:\java\mypackage\AccountBalance.java
C:\java\mypackage\Balance.java
C:\java\mypackage\Thing.java
package mypackage;
import mypackage.Thing; // unnecessary import, NOTE : name needs to be qualified w/ package
class AccountBalance {
public static void main(String args[]) {
Balance balance = new Balance("Jack", 123); // SAME package --> no need to import
balance.show();
Thing thing = new Thing("Jill");
System.out.println(thing.getName());
}
}
Balance.java
package mypackage;
class Balance {
String name;
double balance;
Balance (String name, double balance) {
this.name = name;
this.balance = balance;
}
void show() {
if (balance >0) {
System.out.print("-->");
System.out.println(name + " : $" + balance);
}
}
}
Thing.java
package mypackage;
class Thing {
String name;
Thing(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
C:\java\mypackage> javac AccountBalance.java Balance.java Thing.java
C:\java\mypackage> cd ..
C:\java> java mypackage.AccountBalance
Notes on Execution :
AccountBalance.java
file and compile just by doing javac AccountBalance.java
but that gets messy as the files grow.mypackage.AccountBalance
to "name" everything correctly. AccountBalance
doesn't exist from the run time's perspective. Nor does mypackage.AccountBalance.java
Upvotes: 0
Reputation: 6378
The import statement is used to import classes from packages other than the package which your class is. You do not need to import packages if the other classes you use are in the same package.
Upvotes: 0
Reputation: 2900
You don't need to import classes in the same package as the current class, they're automatically imported.
Upvotes: 2