Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 953381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:02:35+00:00 2026-05-16T00:02:35+00:00

When do we actually use the package keyword? What does it mean? Suppose I

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:02:36+00:00Added an answer on May 16, 2026 at 12:02 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.