Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package in Java

Tags:

java

When do we actually use the package keyword? 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

like image 311
masnun Avatar asked Jul 24 '10 13:07

masnun


1 Answers

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 into my.proj.main
  • Dice.java is packaged into my.proj.sims
  • Main.java needs to import my.proj.sims.Dice in order to create a Dice object and use its methods because it's in a different package from Dice.java.
  • DiceRoller.java does not need to import my.proj.sims.Dice because it is in the same package as Dice.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.

.

Hope this clears things up. Please consider upvoting if this has helped you :)

like image 119
Justian Meyer Avatar answered Oct 11 '22 08:10

Justian Meyer