Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a static local class not allowed in a method? [closed]

Tags:

java

oop

class

I've been brushing off my java and I've some misunderstanding about local classes (that I ultimately never used), I well understand the concept of static but not in the case of local classes.

1. Why is a static method not allowed in a local classes ?

2. Why is a static local class not allowed in a method ?

  1. A static method not allowed in a local classes:

Here I don't get it. To me the local class is tied to the static method main. I just don't understand why this cannot be done. The method main is accessed through the Sequence class and then since sayGoodbye is static it should be accessed through its class. But no.

public class Sequence {

    public static void main(String... args) {

        class EnglishGoodbye {
            public static void sayGoodbye() { // this cannot be done
                System.out.println("Bye bye");
            }
        }
        EnglishGoodbye.sayGoodbye();
    }
}
  1. A static local class not allowed in a method :

This cannot be done: It's a bit ambiguous but I'd think a static here would have the same meaning as a non static since the static class is tied to a static method. I'm confused.

public class Sequence {

    public static void main(String... args) {

        static class EnglishGoodbye { //static local classes not allowed
            public static void sayGoodbye() {
                System.out.println("Bye bye");
            }
        }
        EnglishGoodbye.sayGoodbye();
    }
}

Edit: The first answer I got was a quote from oracle :

Local classes are non-static because they have access to instance members of the enclosing block. Consequently, they cannot contain most kinds of static declarations.

and my reply :

That doesn't really explain everything though. When you have an inner class you can't access non static fields but you can access static fields. Same should apply for a local class, and since there is no static variable then it's useless. But what about methods, like in my example.

Okay I made a schema to better explain how I view things. It might be totally erroneous though and I'm a bit ashamed to show it. In this schema and in the scenario where a static local class would be accessible I'd have a local class in the top memory block. Whenever the static method2 would be called it would simply reference to it.

enter image description here

like image 664
Ced Avatar asked Jun 04 '16 21:06

Ced


1 Answers

There are two kinds of classes in Java: Top-Level and Nested.
There are two kinds of Nested classes: Static Nested and Inner.
There are also two special kinds of Inner classes: Local and Anonymous.

Local and Anonymous classes are by definition Inner classes, i.e. non-static.

See The Java™ Tutorials - Local Classes Are Similar To Inner Classes:

Local classes are non-static because they have access to instance members of the enclosing block. Consequently, they cannot contain most kinds of static declarations.

But, you already saw that, so let me quote JLS §14.3 Local Class Declarations:

All local classes are inner classes (§8.1.3).


Reasoning (my opinion)

What is the point of the first example?

public static void main(String... args) {
    class EnglishGoodbye {
        public static void sayGoodbye() { // this cannot be done
            System.out.println("Bye bye");
        }
    }
    EnglishGoodbye.sayGoodbye();
}

Just make the method a private static of the main class:

public static void main(String... args) {
    sayGoodbye();
}
public static void sayGoodbye() {
    System.out.println("Bye bye");
}

Oh, did you want to access variables and parameters from the method?

public static void main(String... args) {
    final String message = "Bye bye";
    class EnglishGoodbye {
        public static void sayGoodbye() { // this cannot be done
            System.out.println(message);
        }
    }
    EnglishGoodbye.sayGoodbye();
}

Problem with that is that a static method has no instance context, so which instance of message would that be?

To specify instance, a new EnglishGoodbye() statement is needed, and the compiler will add hidden instance fields to EnglishGoodbye to represent the value of message, which is also why message has to be (effectively) final, since it is copying the value of the variable. Remember, unlike C, you cannot reference a variable by pointer.

public static void main(String... args) {
    final String message = "Bye bye";
    class EnglishGoodbye {
        public void sayGoodbye() {
            System.out.println(message);
        }
    }
    new EnglishGoodbye().sayGoodbye();
}

Same goes for the second example. What is the point?

public static void main(String... args) {
    static class EnglishGoodbye { //static local classes not allowed
        public static void sayGoodbye() {
            System.out.println("Bye bye");
        }
    }
    EnglishGoodbye.sayGoodbye();
}

Just make the class a private static of the main class:

public static void main(String... args) {
    EnglishGoodbye.sayGoodbye();
}
private static class EnglishGoodbye {
    public static void sayGoodbye() {
        System.out.println("Bye bye");
    }
}

Same reasoning as above for first example, if you has intended to access method variables and parameters. The class needs an instance, to know which instance of the variables/parameters to access.


Not that it's directly related to answer, but I made this, so might as well keep it.

Here is the Java type system as a hierarchy (not to be confused with inheritance/subtypes):

  • Type (§4)
    • Primitive type (§4.2)
      • boolean, byte, short, int, long, char, float, double
    • Reference type (§4.3)
      • null type (§4.1)
      • Type variable (§4.4),
      • Array type (§10.1)
      • Interface (§9.1)
        • Annotation type (§9.6)
        • Normal interface
          • Top-level interface (§7.6)
          • Nested interface (§8.5, §9.5)
      • Class (§8.1)
        • Enum type (§8.9)
        • Normal class
          • Top-Level class (§7.6)
          • Nested class (§8.5, §9.5)
            • Static Nested class
            • Inner class (§8.1.3)
              • Local class (§14.3)
              • Anonymous class (§15.9.5)
like image 177
Andreas Avatar answered Nov 09 '22 12:11

Andreas