简体   繁体   English

为什么Java中的本地类不能具有静态方法?

[英]Why local classes in Java can`t have static methods?

In method main is class Counter that have method quantity that returns quantiny of numbers in given String. 方法主类中的Counter类具有返回给定String中数字的数量的方法数量。

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

    class Counter{
        static final String numbers="0123456789";
        int quantity(String main){
           int i=0;
            for (char c:main.toCharArray()){
                if (numbers.contains(Character.toString(c)))
                i++;
                }
         return i;
        }
    }

    String str="a0b3s2d6";
    Counter c=new Counter();
    System.out.println(c.quantity(str));
}

I think that creating instance of Counter is uselles, calling just Counter.quantity(str) will be easier than creating instance. 我认为创建Counter实例很麻烦,只调用Counter.quantity(str)比创建实例要容易。 So Why we can't have static methods in local classes? 那么为什么我们在本地类中不能有静态方法呢?

Local classes can be only created when the parent class is initialized. 只能在初始化父类时创建本地类。 Mentioning them static means that they should be created before the class initialization which wouldn't be possible. 静态提及它们意味着应该在类初始化之前创建它们,而这是不可能的。

For more details , see this 有关更多详细信息,请参阅

Static : static keyword to create fields and methods that belong to the class, rather than to an instance of the class.We make members and methods static so that it can be used elsewhere and called from anywhere by using class names but static:static关键字,用于创建属于该类而不是该类实例的字段和方法。我们将成员和方法设为静态,以便可以在其他位置使用它并可以通过使用类名在任何地方调用它,

If we will make members of inner class static still we wont be able to access it outside that class. 如果我们将内部类的成员设为静态,我们将无法在该类外部访问它。

In this case an inner class is associated with an instance of its outer class, it cannot define any static methods itself. 在这种情况下,内部类与其外部类的实例相关联,它本身不能定义任何静态方法。 A static nested class cannot refer directly to instance variables or methods defined in its enclosing class, it can use them only through an object reference. 静态嵌套类不能直接引用其封闭类中定义的实例变量或方法,它只能通过对象引用来使用它们。

Because static methods could be called from anywhere of your code. 因为可以从代码的任何位置调用静态方法。

How would you call static method of the class Counter that you could not reach because it is in the local scope of some other class? 您将如何调用Counter类的静态方法,因为它在某些其他类的本地范围内而无法到达?

You're doing something wrong. 您做错了。

UPD UPD

Why you trying to instantiate your static class? 为什么要尝试实例化静态类?

Counter c=new Counter();

What goal are you actually trying to reach? 您实际上要达到什么目标?

If you want call the method like Counter.quantity(str) ,You can define the class out the main method. 如果要调用Counter.quantity(str)之类的方法,则可以在main方法中定义类。

public static void main(String... args) {
    String str = "a0b3s2d6";
    System.out.println(Counter.quantity(str));
}

static class Counter {
    static final String numbers = "0123456789";

    static int quantity(String main) {
        int i = 0;
        for (char c : main.toCharArray()) {
            if (numbers.contains(Character.toString(c)))
                i++;
        }
        return i;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM