Skip to lesson
MentorNxt home

Java Programming

Packages, Imports & Access Control

Create your own package

One line at the top of a file puts its class in a package: package com.college.students;. The file must also sit in the matching folder, com/college/students.

The package statement

Your Welcome class should belong to the package com.college.students. The package statement, the word package, the package name and a semicolon, does that. It must be the first line of code in the file.

Example 1 · A class in a package

Welcome.javaJava
package com.college.students;
public class Welcome {    public static void main(String[] args) {        System.out.println("Welcome to the college app");        System.out.println("This class is in com.college.students");    }}
Sample output
Welcome to the college app
This class is in com.college.students

What this code does

  • Line 1 puts Welcome in com.college.students, so its full name is com.college.students.Welcome.
  • Only comments may come above the package line, and a file has at most one.

A file with no package line is in the default package, a package with no name. All your programs so far were there. That is fine for practice; real apps use named packages.

Find the bug

Fix the package line

Select the line that is wrong.

This file should put Topper in the package com.college.students.

Round 1 of 4

Folders follow the package name

Where do you save Welcome.java? Each part of the package name is one folder, so the file goes in src/com/college/students/. The command javac -d out then builds the same folders for the class files.

Left, your code: src/com/college/students/Welcome.java. An arrow labelled javac -d out leads to the right, the class files: out/com/college/students/Welcome.class. javac reads the package line and makes the same folders inside out.
Write package names in lowercase. Class names still start with a capital letter.

Run by the full name

After compiling, java Welcome does not work any more. A class in a package is run by its full name, and -cp out (the class path) tells Java which folder holds the class files.

Example 2 · Compile and run from the terminal

Terminal
javac -d out src/com/college/students/Welcome.javajava -cp out com.college.students.Welcome
Output
Welcome to the college app
This class is in com.college.students

What this code does

  • javac -d out writes out/com/college/students/Welcome.class, as in the picture.
  • java -cp out + the full name runs it. Run both commands in the folder that holds src.

Example 3 · Running with the short name fails

Terminal
java -cp out Welcome
Sample output
Error: Could not find or load main class Welcome
Caused by: java.lang.ClassNotFoundException: Welcome

What this code does

  • Java looks for a class named just Welcome with no package, and there is no such class in out.
  • Fix: run java -cp out com.college.students.Welcome.

For several files, list each one after javac -d out, separated by spaces. Then run the class that has main.

Two classes in one package

StudentApp needs the grade that a method of MarksUtil works out. Classes in the same package use each other directly, with no import and no full name.

Example 4 · Two classes in one package

MarksUtil.javaJava
// File: src/com/college/students/MarksUtil.javapackage com.college.students;
public class MarksUtil {    public static String grade(int marks) {        if (marks >= 75) {            return "A";        } else if (marks >= 50) {            return "B";        }        return "C";    }}
// File: src/com/college/students/StudentApp.javapackage com.college.students;
public class StudentApp {    public static void main(String[] args) {        System.out.println("Asha: " + MarksUtil.grade(86));        System.out.println("Ravi: " + MarksUtil.grade(58));    }}
Sample output
Asha: A
Ravi: B

What this code does

  • Both files start with package com.college.students;, so StudentApp calls MarksUtil.grade(86) with no import.
  • Compile both: javac -d out src/com/college/students/MarksUtil.java src/com/college/students/StudentApp.java.
  • Run the class that has main: java -cp out com.college.students.StudentApp.

Create a package in an IDE

An IDE makes the folders and the package line for you. You still run the class with the Run button, as before.

  • IntelliJ IDEA: right-click src → New → Package, type com.college.students. Then right-click the package → New → Java Class.
  • Eclipse: right-click src → New → Package, then right-click the package → New → Class.
  • VS Code: create the folders com/college/students inside src, then add the file. Check that the first line is the package line; add it if it is missing.

Next step

Check your understanding

8 quick questions