---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 27 Edition"
java_version: 27
chapter: "Chapter 8. Classes"
section: "8.4.8.2"
heading: "8.4.8.2. Hiding (by Class Methods)"
parent: "8.4.8. Inheritance, Overriding, and Hiding"
source: https://docs.oracle.com/javase/specs/jls/se27/html/jls-8.html#jls-8.4.8.2
id: "8.4.8.2"
url: https://jvm.obfuscated.software/jls/se27/8.4.8.2.html
raw: https://jvm.obfuscated.software/jls/se27/8.4.8.2.md
---

# 8.4.8.2. Hiding (by Class Methods)

If a class C declares or inherits a `static` method `m`, then `m` is said to *hide* any method `m'` declared in a class or interface A for which all of the following are true:

- A is a superclass or superinterface of C.
- If A is an interface, `m'` is an instance method.
- `m'` is accessible to C ([§6.6](6.6.md)).
- The signature of `m` is a subsignature ([§8.4.2](8.4.2.md)) of the signature of `m'` as a member of the supertype of C that names A.

It is a compile-time error if a `static` method hides an instance method.

In this respect, hiding of methods differs from hiding of fields ([§8.3](8.3.md)), for it is permissible for a `static` variable to hide an instance variable. Hiding is also distinct from shadowing ([§6.4.1](6.4.1.md)) and obscuring ([§6.4.2](6.4.2.md)).

A hidden method can be accessed by using a qualified name or by using a method invocation expression ([§15.12](15.12.md)) that contains the keyword `super` or a cast to a superclass type.

In this respect, hiding of methods is similar to hiding of fields.

**Example 8.4.8.2-1. Invocation of Hidden Class Methods**

A class (`static`) method that is hidden can be invoked by using a reference whose type is the type of the class that actually contains the declaration of the method. In this respect, hiding of `static` methods is different from overriding of instance methods. The example:

```
class Super {
    static String greeting() { return "Goodnight"; }
    String name() { return "Richard"; }
}
class Sub extends Super {
    static String greeting() { return "Hello"; }
    String name() { return "Dick"; }
}
class Test {
    public static void main(String[] args) {
        Super s = new Sub();
        System.out.println(s.greeting() + ", " + s.name());
    }
}
```

produces the output:

```
Goodnight, Dick
```

because the invocation of `greeting` uses the type of `s`, namely `Super`, to figure out, at compile time, which class method to invoke, whereas the invocation of `name` uses the class of `s`, namely `Sub`, to figure out, at run time, which instance method to invoke.
