为什么方法中不允许使用静态本地类? [英] Why is a static local class not allowed in a method?

查看:147
本文介绍了为什么方法中不允许使用静态本地类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在刷掉我的java并且我对本地类(我最终从未使用过)有一些误解,我很清楚静态的概念但不是

解决方案

Java中有两种类:顶级和嵌套。

有两种嵌套类:静态嵌套和内部。

还有两种特殊的内部类:本地和匿名。



根据定义,本地和匿名类 内部类,即非静态。



参见 Java™教程 - 本地类与内部类相似


本地类是非静态的,因为它们可以访问封闭块的实例成员。因此,它们不能包含大多数静态声明。


但是,你已经看到了,所以让我引用JLS§14.3本地类声明


所有本地类都是内部类(§8.1.3




< hr>

推理(我的意见)



第一个例子有什么意义?


  public static void main(String ... args){
class EnglishGoodbye {
public static void sayGoodbye(){//这不可以做
System.out.println(Bye bye);
}
}
EnglishGoodbye.sayGoodbye();
}


只需将方法设为私有静态主类:

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

哦,你想从方法中访问变量和参数吗?

  public static void main(String ... args){
final String message =Bye bye;
class EnglishGoodbye {
public static void sayGoodbye(){//这不可以做
System.out.println(message);
}
}
EnglishGoodbye.sayGoodbye();
}

问题是静态方法没有实例上下文,那么 message 的实例是什么?



指定实例,需要新的EnglishGoodbye()语句,编译器会将隐藏的实例字段添加到 EnglishGoodbye 来表示值消息,这也是消息必须(有效)最终的原因,因为它是复制变量的值。请记住,与C不同,您不能通过指针引用变量。

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

第二个例子也是如此。有什么意义?


  public static void main(String ... args){
static class EnglishGoodbye {//不允许静态本地类
public static void sayGoodbye(){
System.out.println(Bye bye);
}
}
EnglishGoodbye.sayGoodbye();
}


只需将班级设为私有静态主类:

  public static void main(String ... args){
EnglishGoodbye.sayGoodbye();
}
私有静态类EnglishGoodbye {
public static void sayGoodbye(){
System.out.println(Bye bye);
}
}

与第一个例子相同的推理,如果你有旨在访问方法变量和参数。该类需要一个实例,以了解要访问的变量/参数的哪个实例。






不是它的与答案直接相关,但我做了这个,所以不妨保留它。



这是Java类型系统作为层次结构(不是与继承/子类型混淆)




  • 类型§4


    • 原始类型§4.2


      • boolean byte short int long char float double


    • 参考类型§4.3




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.

解决方案

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):

这篇关于为什么方法中不允许使用静态本地类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆