Java 简明教程
Java - Static Classes
在 Java 中,静态类概念见于 inner classes 的概念,它专门设计用于 class 中一些精细功能。
In Java concept of static class is introduced under concept of inner classes, which are specially designed for some delicate functionality in a class.
Java Static Classes
Java 中的静态类仅允许在其他类下定义的内部类,因为不允许静态外部类,这意味着我们不能对外部类使用静态关键字。
Static classes in Java are allowed only for inner classes which are defined under some other class, as static outer class is not allowed which means that we can’t use static keyword with outer class.
静态类与 Java 中的其他内部类定义相同,只是在其名称前面有一个静态关键字。这些类有一些独特的特性,使它们与其他非静态内部类不同。
Static classes are defined the same as other inner classes in Java only with a static keyword in front of its name. These classes have some unique characteristics that make them differ from other non-static inner classes.
Features of Java Static Classes
以下是 Java 中静态类的一些特性:
The following are the features of static classes in Java:
-
Static class do not need to create an instance of outer containing class in order to create its own instance.
-
Static class can access members(variables/methods) of outer containing class only if they are static in nature.Which means that a static nested class does not have access to the instance variables and methods of the outer class.
Syntax of Java Static Class
静态嵌套类的语法如下 −
The syntax of static nested class is as follows −
class MyOuter {
static class Nested_Demo {
}
}
实例化静态嵌套类与实例化内部类有点不同。以下程序演示了如何在多种情况下使用静态嵌套类。
Instantiating a static nested class is a bit different from instantiating an inner class. The following programs show how to use a static nested class in multiple cases.
Example of Java Static Class
在这个示例中,我们创建了一个类 Outer 和一个内部静态类 NestedDemo。在 main() 方法中,我们直接使用静态类静态方法,而没有引用,因为 main 是 Outer 类的一部分。
In this example, we’ve created a class Outer and an inner static class as NestedDemo within it. In main() method, we’re using static method of static class directly without any reference as main is part of Outer class.
package com.tutorialspoint;
public class Outer {
static class NestedDemo {
public static void print() {
System.out.println("This is my nested class");
}
}
public static void main(String args[]) {
NestedDemo.print();
}
}
如果您编译并执行上述程序,将得到以下结果 −
If you compile and execute the above program, you will get the following result −
Output
This is my nested class
Java Static Class: More Examples
Example 1
在这个示例中,我们创建了一个类 Outer 和一个内部静态类 NestedDemo。由于 main 方法在不同的类中,我们使用 Outer 类访问静态内部类。
In this example, we’ve created a class Outer and an inner static class as NestedDemo within it. As main method is in different class, we’re accessing the static inner class using Outer class.
package com.tutorialspoint;
public class Tester {
public static void main(String[] arguments) {
Outer.NestedDemo.print();
}
}
class Outer {
static class NestedDemo {
public static void print() {
System.out.println("This is my nested class");
}
}
}
如果您编译并执行上述程序,将得到以下结果 −
If you compile and execute the above program, you will get the following result −
Output
This is my nested class
Example 2
在这个示例中,我们创建了一个类 Outer 和一个内部静态类 NestedDemo。现在,由于静态类具有实例方法,并且 main 方法在不同的类中,我们使用 Outer 类对象访问静态内部类。
In this example, we’ve created a class Outer and an inner static class as NestedDemo within it. Now as static class has instance method and main method is in different class, we’re accessing the static inner class using Outer class object.
package com.tutorialspoint;
public class Tester {
public static void main(String[] arguments) {
new Outer.NestedDemo().print();
}
}
class Outer {
static class NestedDemo {
public void print() {
System.out.println("This is my nested class");
}
}
}
如果您编译并执行上述程序,将得到以下结果 −
If you compile and execute the above program, you will get the following result −
Output
This is my nested class