java中有静态类吗?
这样的课有什么意义。静态类的所有方法也需要是静态的吗?
是否反过来要求,如果一个类包含所有静态方法,那么这个类也应该是静态的?
静态类有什么好处?
java中有静态类吗?
这样的课有什么意义。静态类的所有方法也需要是静态的吗?
是否反过来要求,如果一个类包含所有静态方法,那么这个类也应该是静态的?
静态类有什么好处?
当前回答
有一个静态嵌套类,这个[静态嵌套]类不需要一个外围类的实例来实例化自己。
这些类[静态嵌套类]只能访问外围类的静态成员[因为它没有任何对外围类实例的引用…]
代码示例:
public class Test {
class A { }
static class B { }
public static void main(String[] args) {
/*will fail - compilation error, you need an instance of Test to instantiate A*/
A a = new A();
/*will compile successfully, not instance of Test is needed to instantiate B */
B b = new B();
}
}
其他回答
好吧,Java有“静态嵌套类”,但它们与c#的静态类完全不一样,如果你是从那里来的的话。静态嵌套类只是一个没有隐式引用外部类实例的类。
静态嵌套类可以有实例方法和静态方法。
在Java中没有顶级静态类这种东西。
都是很好的答案,但是我没有看到java.util.Collections的引用,它为它们的静态因子方法使用了大量的静态内部类。加上相同的。
从java.util.Collections中添加一个示例,其中有多个静态内部类。内部类对于需要通过外部类访问的代码进行分组非常有用。
/**
* @serial include
*/
static class UnmodifiableSet<E> extends UnmodifiableCollection<E>
implements Set<E>, Serializable {
private static final long serialVersionUID = -9215047833775013803L;
UnmodifiableSet(Set<? extends E> s) {super(s);}
public boolean equals(Object o) {return o == this || c.equals(o);}
public int hashCode() {return c.hashCode();}
}
下面是java.util.Collections类中的静态因子方法
public static <T> Set<T> unmodifiableSet(Set<? extends T> s) {
return new UnmodifiableSet<>(s);
}
简单地说,Java只支持将一个类声明为内部类的静态,而不支持将顶级类声明为静态。
顶级类:一个java项目在每个java源文件中可以包含多个顶级类,其中一个类以文件名命名。在顶级类前面只允许有三个选项或关键字,public、abstract和final。
内部类:在顶级类内部的类称为内部类,这基本上是嵌套类的概念。内部类可以是静态的。使内部类静态的想法是利用实例化内部类的对象而不实例化顶层类的对象的优势。这与顶级类中的静态方法和变量的工作方式完全相同。
因此Java支持内部类级别的静态类(在嵌套类中)
Java不支持顶级类的静态类。
我希望这可以为基本理解Java中的静态类提供一个更简单的解决方案。
有一个静态嵌套类,这个[静态嵌套]类不需要一个外围类的实例来实例化自己。
这些类[静态嵌套类]只能访问外围类的静态成员[因为它没有任何对外围类实例的引用…]
代码示例:
public class Test {
class A { }
static class B { }
public static void main(String[] args) {
/*will fail - compilation error, you need an instance of Test to instantiate A*/
A a = new A();
/*will compile successfully, not instance of Test is needed to instantiate B */
B b = new B();
}
}
Java有静态嵌套类,但听起来像是在寻找顶级静态类。Java没有办法让顶级类成为静态的,但是你可以像这样模拟一个静态类:
Declare your class final - Prevents extension of the class since extending a static class makes no sense Make the constructor private - Prevents instantiation by client code as it makes no sense to instantiate a static class Make all the members and functions of the class static - Since the class cannot be instantiated no instance methods can be called or instance fields accessed Note that the compiler will not prevent you from declaring an instance (non-static) member. The issue will only show up if you attempt to call the instance member
以上建议的简单例子:
public class TestMyStaticClass {
public static void main(String []args){
MyStaticClass.setMyStaticMember(5);
System.out.println("Static value: " + MyStaticClass.getMyStaticMember());
System.out.println("Value squared: " + MyStaticClass.squareMyStaticMember());
// MyStaticClass x = new MyStaticClass(); // results in compile time error
}
}
// A top-level Java class mimicking static class behavior
public final class MyStaticClass {
private MyStaticClass () { // private constructor
myStaticMember = 1;
}
private static int myStaticMember;
public static void setMyStaticMember(int val) {
myStaticMember = val;
}
public static int getMyStaticMember() {
return myStaticMember;
}
public static int squareMyStaticMember() {
return myStaticMember * myStaticMember;
}
}
What good are static classes? A good use of a static class is in defining one-off, utility and/or library classes where instantiation would not make sense. A great example is the Math class that contains some mathematical constants such as PI and E and simply provides mathematical calculations. Requiring instantiation in such a case would be unnecessary and confusing. See the Math class and source code. Notice that it is final and all of its members are static. If Java allowed top-level classes to be declared static then the Math class would indeed be static.