Java

Calling Java Methods From Another Class: A Clear Walkthrough

Calling a method that lives in another class is the most routine operation in Java. It is also the operation where the access modifiers — `public`, `private`, `protected`, default — decide what compiles and what does not. A tutorial on CodeUnderscored walks through the three common cases with sample code.

The rules are simpler than people make them. They are also more annoying than they look.

Quick take

  • A method with no modifier is package-private (default), accessible only inside the same package.
  • `public` methods are accessible from any class, with or without an object.
  • `static` methods belong to the class, not an instance, and are called with `ClassName.method()`.
  • `protected` methods are accessible from subclasses and from the same package.
  • `private` methods are never callable from another class.

Calling a default method from another class

A method with no access modifier is treated as default — package-private. It is more accessible than `private` but more restrictive than `protected` or `public`. You can call it from another class only if that class lives in the same package.

The sample in the tutorial uses a `School` class with a `getName()` method and a `Testing` class that creates a `School` object and calls `school.getName()`. Output: `The school name is : Cheptikit`. The dot syntax works because both classes are in the same package.

Calling a static method from another class

Static methods belong to the class, not to an instance. You call them with `ClassName.methodName()` and you do not create an object first. The trade-off is that static methods access static data directly; reaching instance fields still requires an object.

The static sample uses the same `School` class but declares `name` and `getName()` as `static`. The caller writes `School.name = "Cheptikit";` and `School.getName()`. Output: `The School name is : Cheptikit`. No `new` keyword, no instance.

Calling a protected method from another class

`protected` sits between package-private and public. It is accessible from any class in the same package, and from any subclass even if the subclass is in a different package.

The post begins to walk through the protected case (the snippet in the reference is cut off), but the rule of thumb is: if the calling class extends the owning class, the protected method is in scope. If it does not, the calling class needs to be in the same package or the method needs to be `public`.

Our analysis

The real friction in Java method access is not the syntax — it is the implicit default. A method declared without a modifier is package-private, which is rarely what beginners expect. New developers tend to assume "no modifier" means "accessible everywhere" because they have not yet hit the wall of a multi-package project. The wall is where these rules suddenly matter.

The other trap is mixing static and instance access. Trying to reach an instance field from a static context is one of the most common compile errors in beginner Java code. The fix is either to make the field static or to call the method through an object.

What to watch next: Java's module system (JPMS) layers another access rule on top of these. If you start using `module-info.java`, even `public` classes can be hidden from other modules unless explicitly exported.

Worked example: cross-class access in a real package layout

Say you have two packages: `com.school.core` with `Student` and `com.school.reports` with `ReportGenerator`. `Student.getScore()` is `public`, so `ReportGenerator` can call `student.getScore()` after creating a `Student` object. `Student.internalNote()` is default, so `ReportGenerator` cannot call it unless you move it to the `com.school.core` package or change the modifier to `public`. That single decision is the kind of thing that wastes an afternoon when you do not know the rule.

Bottom line

Memorize the four modifiers in order from most open to most closed: `public`, `protected`, default (no modifier), `private`. That is the entire access ladder. Everything else is just remembering which method on which class you meant to call.

Key takeaways

  • Default (no modifier) means package-private, not "accessible everywhere."
  • Static methods are called with `ClassName.method()`, no object required.
  • Protected methods open up to subclasses and same-package classes only.

Frequently asked questions

Can I call a default method from any class in my project?

No. Default (package-private) methods are only accessible from classes in the same package.

Do I need to create an object to call a static method?

No. Static methods are called with `ClassName.methodName()` directly.

Why can't my subclass see a private method in its parent?

Private methods are not inherited and are not accessible outside the class that declares them.

What is the difference between protected and default?

Protected methods are accessible from subclasses in any package, while default methods are only accessible from the same package.

Can a static method access instance fields directly?

No. Static methods access static data directly and need an object to reach instance fields.

Related