Package in Java
When do we actually use the package keywor开发者_如何学JAVAd? What does it mean?
Suppose I write the following code:
package masnun;
public class masnun{
public static void main(String args[]) {
System.out.println("Hello maSnun!");
}
}
What does this package thing do? I get a masnun.class file that doesn't run. I am new to Java. Can somebody please explain?
Thanks
As I'm not a fan of these other answers, I'll write my own.
Real World Examples:
Think of a "package" as an easy way for a java class to reference another.
Let's say I have this big box in my attic. I have a calculator, compass, protractor, etc. I can label this box MathTools
.
Another example would be taking all your pictures and putting them in the Pictures
folder in your documents. From there, you could split them into Spring Break 2009
or [Insert Name Here]'s Party
.
How does this relate to Java? Well, let's look at the java.util
package (you can reference this with import java.util.*;
. You have ArrayLists, Strings, Random, etc. which are used in most Java programs (common "utilities", if you prefer). There are all neatly organized into the same package, so that programmers can easily reference them (import java.util.*;
).
Easy Application:
Let's assume that we can find all the files to a small dice simulator in C:/Program Files/Java Project/my/proj/
(it's likely that this file doesn't exist on your computer, but just pretend for a moment).
You have 3 files: Main.java
, Dice.java
, and DiceRoller.java
. All of which are shown below:
"C:/ProgramFiles/Java Project/my/proj/main/Main.java
":
package my.proj.main;
import my.proj.sims.Dice;
public class Main
{
public static void main(String[] args)
{
DiceRoller roller = new DiceRoller();
roller.rollAndShow(4);
}
}
"C:/ProgramFiles/Java Project/my/proj/sims/Dice.java
":
package my.proj.sims;
import java.util.Random; // I used the Random class, but you can also use the Math class if you prefer (java.lang.Math)
public class Dice
{
public Dice()
{
}
public int roll()
{
Random rand = new Random();
return rand.nextInt(6) + 1; // Rolls a random number 1-6
}
}
"C:/ProgramFiles/Java Project/my/proj/sims/DiceRoller.java
":
package my.proj.sims;
public class DiceRoller
{
public DiceRoller ()
{
}
// Rolls and prints the result of 'n' number of rolls
public void rollAndShow(int n)
{
Dice dice = new Dice();
for (int i = 0; i < n; i++)
{
System.out.println(dice.roll()); // You should never use S.o.p in a method - it's bad practice, but it's easier this way if you don't yet understand the concept of objects
}
}
}
Things to notice:
Main.java
is packaged intomy.proj.main
Dice.java
is packaged intomy.proj.sims
Main.java
needs to importmy.proj.sims.Dice
in order to create aDice
object and use its methods because it's in a different package fromDice.java
.DiceRoller.java
does not need to importmy.proj.sims.Dice
because it is in the same package asDice.java
and the compiler will automatically associate the two.
Import
is a command to load the functionality of a class into the current file. Look at Dice.java
, for example. In order for it to create a Random
object, which has the method nextInt()
, it needs to import the Random class from the java.util.*
package.
You might notice that some people would prefer to use java.util.*
instead of java.util.Random
, java.util.ArrayList
, etc. What the *
essentially means is any class within java.util
. Running import java.util.*
will import the Random, String, ArrayList, etc. classes.
It's very likely for a software to have classes named Logger, Database, Connection, Table etc. Packages separate the scope of these classes, so that you don't have issues with duplicate classnames.
A few examples:
- java.*
- org.apache.**
- com.yourcompany.yourproject.
If you wan't to use classes from a destinct package you need to import them .i.g.
import java.util.Date
or
import java.sql.Date
It is such a way to organize your source files. A package contains multiple class file, a class file contains only a public class, a class contain multiple methods and variable.
The package statement in Java is used to group several related classes. If you have classes that deal exclusively with Database interaction, it would be logical to put them all under
com.example.myapp.persistence
This also provides the benefice of name spacing, remember that at runtime part of a java class identity is its fully qualified name, that is, the class name preceded by its package name like in
com.example.myapp.persistence.UserDAO
When you're application grows to accommodate more than a couple of classes, it is both practical and a good practice to organize them in packages
"Packages are nothing more than the way we organize files into different directories according to their functionality, usability as well as category they should belong to." from: http://www.jarticles.com/package/package_eng.html
Basically, in your example, you are saying class masnun is package of package masnun. So essentially in other classes you could reference this class by import masnun; Which imports the class masnun which resides in masnun package.
In Layman term, Lets suppose that there are multiple department in a college like science , arts etc. Now each department has some employee. In terms of object there can be multiple Employee class so there is restriction in java that we can't put two class as same name .So programmer should make different packages to store different class with same name in application.
精彩评论