java中有静态类吗?
这样的课有什么意义。静态类的所有方法也需要是静态的吗?
是否反过来要求,如果一个类包含所有静态方法,那么这个类也应该是静态的?
静态类有什么好处?
java中有静态类吗?
这样的课有什么意义。静态类的所有方法也需要是静态的吗?
是否反过来要求,如果一个类包含所有静态方法,那么这个类也应该是静态的?
静态类有什么好处?
当前回答
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.
其他回答
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.
java中有静态类吗?
单例就像一个静态类。我很惊讶居然没人提起过。
public final class ClassSingleton {
private static ClassSingleton INSTANCE;
private String info = "Initial info class";
private ClassSingleton() {
}
public static ClassSingleton getInstance() {
if(INSTANCE == null) {
INSTANCE = new ClassSingleton();
}
return INSTANCE;
}
// getters and setters
public String getInfo(){
return info;
}
}
用法是这样的:
String infoFromSingleton = ClassSingleton.getInstance().getInfo()
单例非常适合存储数组列表/列表/集合类等…如果您经常从多个区域收集、更新、复制集合,并且需要这些集合保持同步。或者多对一。
除非是内部类,否则不能对类使用static关键字。静态内部类是一个嵌套类,它是外部类的静态成员。可以在不实例化外部类的情况下,使用其他静态成员访问它。就像静态成员一样,静态嵌套类不能访问外部类的实例变量和方法。
public class Outer {
static class Nested_Demo {
public void my_method() {
System.out.println("This is my nested class");
}
}
public static void main(String args[]) {
Outer.Nested_Demo nested = new Outer.Nested_Demo();
nested.my_method();
}
}
是的,在java中有一个静态嵌套类。 当你声明一个嵌套类静态时,它自动成为一个独立的类,可以实例化,而不必实例化它所属的外部类。
例子:
public class A
{
public static class B
{
}
}
因为类B被声明为静态的,你可以显式实例化为:
B b = new B();
注意,如果类B没有被声明为静态以使其独立,实例对象调用将看起来像这样:
A a= new A();
B b = a.new B();
Java中的类可以是静态的吗?
答案是肯定的,我们可以在java中有静态类。在java中,我们有静态实例变量、静态方法和静态块。在Java中,类也可以是静态的。
在java中,我们不能使顶级(外部)类是静态的。只有嵌套类可以是静态的。
静态嵌套类vs非静态嵌套类
1)嵌套静态类不需要外部类的引用,但是 非静态嵌套类或内部类需要外部类 参考。
2)内部类(或非静态嵌套类)既可以访问静态 和Outer类的非静态成员。静态类不能访问Outer类的非静态成员。它只能访问静态 外层阶级的成员。
请看这里:https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html