-1

Usually, we can identify all the classes, methods & data members by seeing their signatures.

Ex: Math.sqrt(), System.out.println

Math - class

sqrt - static method

print/println - instance method

But Not able to identify static methods of nested classes.

package so;

class Outer {
    static String outerVar = "ABC"; 
    class InsInner {
        static void show() {
            System.out.println("Instance inner "+outerVar);
        }
    }
    static class StaticInner {
        static void show() {
            System.out.println("Static inner class static method "+outerVar);
        }
    }
}
public class NestedClass {
    public static void main(String[] args) {
        Outer.InsInner.show();
        Outer.StaticInner.show();
    }
}

Why both static and non static classes have same convention?

Outer.InsInner.show();
Outer.StaticInner.show();
Prabhu
  • 783
  • 2
  • 10
  • 35
  • 1
    Why would they use different conventions? The static attribute of a class is irrelevant in the context of calling a static method. – Davis Herring May 28 '23 at 11:56
  • I hops this link helps, https://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class?rq=2 – Gaurav kedia May 28 '23 at 11:58
  • 1
    'Static inner' is a contradiction in terms, and therefore so is `StaticInner `. It is a static, nested class. Your question is somewhat confused. – user207421 May 28 '23 at 12:49
  • The _inner class_ is _InsInner_, and the _nested class_ is _StaticInner_. The class you have named _NestedInner_ is just another class, it's not nested. – Reilas May 28 '23 at 16:19
  • A similar question, https://stackoverflow.com/a/975173/17758716. – Reilas May 28 '23 at 16:30

1 Answers1

1

Because show() is static in both places. When a method is static it does not require an instance to operate, so you don't need an InsInner or StaticInner to call either show() and the calls look the same. If you make show() an instance method, then it would have to differ. Like,

class Outer {
    static String outerVar = "ABC";

    class InsInner {
        void show() {
            System.out.println("Instance inner " + outerVar);
        }
    }

    static class StaticInner {
        void show() {
            System.out.println("Static inner class static method " + outerVar);
        }
    }

    public static void main(String[] args) {
        new Outer().new InsInner().show();
        new Outer.StaticInner().show();
    }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249