Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

java - Java中的静态类(Static Classes In Java)

Is there anything like static class in java?

(java中是否有类似static class东西?)

What is the meaning of such a class.

(这样的课是什么意思。)

Do all the methods of the static class need to be static too?

(静态类的所有方法是否也必须是static ?)

Is it required the other way round, that if a class contains all the static methods, shall the class be static too?

(是否需要反过来,如果一个类包含所有静态方法,那么该类也应该是静态的吗?)

What are static classes good for?

(静态类有什么用?)

  ask by Kraken translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Java has static nested classes but it sounds like you're looking for a top-level static class.

(Java具有静态嵌套类,但听起来您正在寻找顶级静态类。)

Java has no way of making a top-level class static but you can simulate a static class like this:

(Java无法将顶级类设为静态,但是您可以像这样模拟静态类:)

  • Declare your class final - Prevents extension of the class since extending a static class makes no sense

    (final声明您的课程-阻止扩展该课程,因为扩展静态课程没有意义)

  • Make the constructor private - Prevents instantiation by client code as it makes no sense to instantiate a static class

    (将构造函数设为private -防止通过客户端代码实例化,因为实例化静态类没有意义)

  • 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

    (使类的所有成员和函数static -由于无法实例化该类,因此无法调用任何实例方法或访问实例字段)

  • 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

    (仅当您尝试调用实例成员时才会显示该问题)

Simple example per suggestions from above:

(每个建议的简单示例:)

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.

(一个很好的例子是Math类,它包含一些数学常数,例如PI和E,并且仅提供数学计算。)

Requiring instantiation in such a case would be unnecessary and confusing.

(在这种情况下,需要实例化将是不必要且令人困惑的。)

See the Math class and source code .

(请参阅Math类和源代码 。)

Notice that it is final and all of its members are static .

(注意,它是final ,其所有成员都是static 。)

If Java allowed top-level classes to be declared static then the Math class would indeed be static.

(如果Java允许将顶级类声明为static则Math类确实是静态的。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...