About Us Contact Us Write for Us Advertise
Home > Java > Packages and the static Keyword in Java Explained
Java

Packages and the static Keyword in Java Explained

Understand Java packages and the static keyword — how packages organise code, how import works, static fields and methods, and finally why main is static.

Shiv Pandey
Shiv Pandey
Sep 22, 2026 | 7 views
Packages and the static Keyword in Java Explained

Two small but essential topics in this lesson, and they'll clear up things you've been quietly wondering about. First, packages — how Java organises code into folders so big projects don't turn into chaos. Second, the static keyword — which you've been typing since your very first program without a full explanation. Let's finally demystify both.

Part 1: Packages

A package is simply a folder for your Java classes — a way to group related code together and keep names from clashing. A real application has hundreds of classes; without packages it would be an unmanageable heap. This very website organises its code into packages like web, admin, services, and security — each holding the classes for that part of the app.

Declaring a package

The package line goes at the very top of a file and mirrors the folder structure:

package com.myapp.models;   // this file lives in folder com/myapp/models

public class User {
    // ...
}

The convention is a reversed website domain (com.company.project) so package names are globally unique. It's why you see names like com.gyaanpost.blog in this project.

Importing from other packages

To use a class from another package, you import it — you've done this already with Scanner:

import java.util.Scanner;      // one specific class
import java.util.*;            // everything in java.util

Classes in the same package don't need importing — Java finds them automatically. And a few core classes (like String and System, from java.lang) are imported for you invisibly, which is why you never had to import String.

Part 2: The static keyword

Here's the mystery solved. Normally, fields and methods belong to an object — you need to create one with new to use them. But static means something belongs to the class itself, not to any single object. You use it directly on the class, no object needed.

Static fields — shared by all objects

A static field has one single copy shared across every object of the class. Perfect for something like a counter of how many objects exist:

class Student {
    static int count = 0;   // ONE shared copy for the whole class
    String name;

    Student(String name) {
        this.name = name;
        count++;             // every new student bumps the shared count
    }
}

new Student("Asha");
new Student("Ravi");
System.out.println(Student.count);   // 2 — accessed on the CLASS, not an object

Notice Student.count — we use the class name, because the field belongs to the class, not to Asha or Ravi individually.

Static methods — no object required

A static method can be called straight on the class. Utility/helper methods are the classic use:

class MathHelper {
    static int square(int n) {
        return n * n;
    }
}

int result = MathHelper.square(5);   // 25 — no 'new MathHelper()' needed

You've been using static methods all along: Math.max(3, 7), Integer.parseInt("42") — all called directly on the class.

Why main is static — the mystery solved!

Remember public static void main(String[] args)? Now the static makes sense: when your program starts, no objects exist yet. Java needs to call main without creating an object first — and static is exactly what lets it do that. Mystery from Lesson 3 officially solved. ✅
One rule to remember: a static method can't directly use non-static (object) fields, because there might not be any object around. If you try, you'll get "non-static variable cannot be referenced from a static context" — one of the most common beginner errors. The fix is usually to make the thing static too, or to work with an actual object.

The static keyword confused me for ages until it clicked as "belongs to the class, not to one object." Once I had that sentence, a whole category of errors and half-understood code suddenly made sense — including why main had to be static all along. If you remember just that one line, you're 90% of the way there.

Key takeaways

  • A package organises classes into folders; declare with package, use others with import.
  • static means "belongs to the class, not an object" — use it via the class name.
  • A static field is shared by all objects; a static method needs no object.
  • main is static so Java can run it before any objects exist.

← Previous: Lesson 17 — Abstraction & Interfaces
Next: Lesson 19 — this, super & final →

Related Articles

this, super and final in Java: Three Keywords Explained
Java

this, super and final in Java: Three Keywords Explained

Abstraction in Java: Abstract Classes and Interfaces Explained
Java

Abstraction in Java: Abstract Classes and Interfaces Explained

Polymorphism in Java: One Method, Many Forms Explained Simply
Java

Polymorphism in Java: One Method, Many Forms Explained Simply

Java Methods Explained: Reusable Code with Parameters and Return Values
Java

Java Methods Explained: Reusable Code with Parameters and Return Values