为什么在Java中不能将类声明为静态?


当前回答

所以,我来晚了,但这是我的两种观点——哲学上补充科林·赫伯特的答案。

在高层次上,你的问题涉及对象和类型之间的区别。虽然有很多Car(对象),但只有一个Car类(类型)。将某些内容声明为静态意味着您在“类型”空间中操作。只有一个。顶级类关键字已经在“type”空间中定义了一个类型。因此,“公共静态类Car”是多余的。

其他回答

只有嵌套类可以是静态的。通过这样做,您可以使用嵌套类而无需外部类的实例。

class OuterClass {
    public static class StaticNestedClass {
    }

    public class InnerClass {
    }

    public InnerClass getAnInnerClass() {
        return new InnerClass();
    }

    //This method doesn't work
    public static InnerClass getAnInnerClassStatically() {
        return new InnerClass();
    }
}

class OtherClass {
    //Use of a static nested class:
    private OuterClass.StaticNestedClass staticNestedClass = new OuterClass.StaticNestedClass();

    //Doesn't work
    private OuterClass.InnerClass innerClass = new OuterClass.InnerClass();

    //Use of an inner class:
    private OuterClass outerclass= new OuterClass();
    private OuterClass.InnerClass innerClass2 = outerclass.getAnInnerClass();
    private OuterClass.InnerClass innerClass3 = outerclass.new InnerClass();
}

来源:

关于嵌套类的Oracle教程

关于同一话题:

Java:静态与非静态内部类 Java内部类和静态嵌套类

In addition to how Java defines static inner classes, there is another definition of static classes as per the C# world [1]. A static class is one that has only static methods (functions) and it is meant to support procedural programming. Such classes aren't really classes in that the user of the class is only interested in the helper functions and not in creating instances of the class. While static classes are supported in C#, no such direct support exists in Java. You can however use enums to mimic C# static classes in Java so that a user can never create instances of a given class (even using reflection) [2]:

public enum StaticClass2 {
    // Empty enum trick to avoid instance creation
    ; // this semi-colon is important

    public static boolean isEmpty(final String s) {
        return s == null || s.isEmpty();
    }
}

您可以在Eclipse中的PlatformUI中查看带有静态方法和私有构造函数的类,该类本身是final的。

public final class <class name>
{
   //static constants
   //static memebers
}

我认为这可能就像喝一杯咖啡一样简单! 看看这个。 定义类时不显式使用static关键字。

public class StaticClass {

    static private int me = 3;
    public static void printHelloWorld() {
       System.out.println("Hello World");
    }



    public static void main(String[] args) {
        StaticClass.printHelloWorld();
        System.out.println(StaticClass.me);
    }
}

这不是静态类的定义吗? 我们只使用绑定到类的函数。 注意,在这种情况下,我们可以在该嵌套中使用另一个类。 看看这个:

class StaticClass1 {

    public static int yum = 4;

    static void  printHowAreYou() {
        System.out.println("How are you?");
    }
}

public class StaticClass {

    static int me = 3; 
    public static void printHelloWorld() {
       System.out.println("Hello World");
       StaticClass1.printHowAreYou();
       System.out.println(StaticClass1.yum);
    }



    public static void main(String[] args) {
        StaticClass.printHelloWorld();
        System.out.println(StaticClass.me);
    }
}

所以,我来晚了,但这是我的两种观点——哲学上补充科林·赫伯特的答案。

在高层次上,你的问题涉及对象和类型之间的区别。虽然有很多Car(对象),但只有一个Car类(类型)。将某些内容声明为静态意味着您在“类型”空间中操作。只有一个。顶级类关键字已经在“type”空间中定义了一个类型。因此,“公共静态类Car”是多余的。