Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As I'm not a fan of these other answers, I'll write my own.</p> <hr> <p><strong>Real World Examples:</strong></p> <p>Think of a "package" as an easy way for a java class to reference another.</p> <p>Let's say I have this big box in my attic. I have a calculator, compass, protractor, etc. I can label this box <code>MathTools</code>. </p> <p>Another example would be taking all your pictures and putting them in the <code>Pictures</code> folder in your documents. From there, you could split them into <code>Spring Break 2009</code> or <code>[Insert Name Here]'s Party</code>.</p> <p>How does this relate to Java? Well, let's look at the <code>java.util</code> package (you can reference this with <code>import java.util.*;</code>. 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 (<code>import java.util.*;</code>).</p> <hr> <p><strong>Easy Application:</strong></p> <p>Let's assume that we can find all the files to a small dice simulator in <code>C:/Program Files/Java Project/my/proj/</code> (it's likely that this file doesn't exist on your computer, but just pretend for a moment).</p> <p>You have 3 files: <code>Main.java</code>, <code>Dice.java</code>, and <code>DiceRoller.java</code>. All of which are shown below:</p> <p>.</p> <p>"C:/ProgramFiles/Java Project/my/proj/main/<code>Main.java</code>":</p> <pre><code>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); } } </code></pre> <p>"C:/ProgramFiles/Java Project/my/proj/sims/<code>Dice.java</code>":</p> <pre><code>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 } } </code></pre> <p>"C:/ProgramFiles/Java Project/my/proj/sims/<code>DiceRoller.java</code>":</p> <pre><code>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 &lt; 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 } } } </code></pre> <p>.</p> <p>Things to notice:</p> <ul> <li><code>Main.java</code> is packaged into <code>my.proj.main</code></li> <li><code>Dice.java</code> is packaged into <code>my.proj.sims</code></li> <li><code>Main.java</code> needs to import <code>my.proj.sims.Dice</code> in order to create a <code>Dice</code> object and use its methods because it's in a different package from <code>Dice.java</code>.</li> <li><code>DiceRoller.java</code> does not need to import <code>my.proj.sims.Dice</code> because it is in the same package as <code>Dice.java</code> and the compiler will automatically associate the two.</li> </ul> <p>.</p> <p><code>Import</code> is a command to load the functionality of a class into the current file. Look at <code>Dice.java</code>, for example. In order for it to create a <code>Random</code> object, which has the method <code>nextInt()</code>, it needs to import the Random class from the <code>java.util.*</code> package.</p> <p>.</p> <p>You might notice that some people would prefer to use <code>java.util.*</code> instead of <code>java.util.Random</code>, <code>java.util.ArrayList</code>, etc. What the <code>*</code> essentially means is any class within <code>java.util</code>. Running <code>import java.util.*</code> will import the Random, String, ArrayList, etc. classes.</p> <p>.</p> <p>Hope this clears things up. Please consider upvoting if this has helped you :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload