ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# Java - 静态类,块,方法和变量 > 原文: [https://beginnersbook.com/2013/04/java-static-class-block-methods-variables/](https://beginnersbook.com/2013/04/java-static-class-block-methods-variables/) `static`关键字可以与类,变量,方法和块一起使用。静态成员属于类而不是特定实例,这意味着如果您将成员设为静态,则可以在不使用对象的情况下访问它。我们举一个例子来理解这个: 这里我们有一个静态方法`myMethod()`,我们可以在没有任何对象的情况下调用这个方法,因为当我们将一个成员静态化时它就变成了类级别。如果我们删除`static`关键字并使其成为非静态关键字,那么我们必须创建一个类的对象才能调用它。 > 静态成员对于类的所有实例(对象)是通用的,但非静态成员对于每个类实例是独立的。 ```java class SimpleStaticExample { // This is a static method static void myMethod() { System.out.println("myMethod"); } public static void main(String[] args) { /* You can see that we are calling this * method without creating any object. */ myMethod(); } } ``` 输出: ```java myMethod ``` ## 静态块 静态块用于初始化静态变量。当在内存中加载类时,将执行此块。一个类可以有多个静态块,它们将按照它们写入程序的相同顺序执行。 ### 示例 1:单个静态块 正如您所看到的,在我们在`main`方法中访问它们之前,两个静态变量都已初始化。 ```java class JavaExample{ static int num; static String mystr; static{ num = 97; mystr = "Static keyword in Java"; } public static void main(String args[]) { System.out.println("Value of num: "+num); System.out.println("Value of mystr: "+mystr); } } ``` 输出: ```java Value of num: 97 Value of mystr: Static keyword in Java ``` ### 示例 2:多个静态块 让我们看看多个静态块如何在 Java 中工作。它们以给定的顺序执行,这意味着第一个静态块在第二个静态块之前执行。这就是原因,第一个块初始化的值被第二个块覆盖。 ```java class JavaExample2{ static int num; static String mystr; //First Static block static{ System.out.println("Static Block 1"); num = 68; mystr = "Block1"; } //Second static block static{ System.out.println("Static Block 2"); num = 98; mystr = "Block2"; } public static void main(String args[]) { System.out.println("Value of num: "+num); System.out.println("Value of mystr: "+mystr); } } ``` **输出:** ```java Static Block 1 Static Block 2 Value of num: 98 Value of mystr: Block2 ``` ## Java 静态变量 静态变量对于类的所有实例(或对象)是通用的,因为它是类级变量。换句话说,您可以说只创建了一个静态变量副本,并在该类的所有实例之间共享。这些变量的内存分配仅在类加载到内存中时才会发生一次。 几点重点: * 静态变量也称为类变量。 * 与**非静态变量**不同,这些变量可以直接在静态和非静态方法中访问。 ### 示例 1:可以在静态方法中直接访问静态变量 这里我们有一个静态方法`disp()`和两个静态变量`var1`和`var2`。这两个变量都可以在静态方法中直接访问。 ```java class JavaExample3{ static int var1; static String var2; //This is a Static Method static void disp(){ System.out.println("Var1 is: "+var1); System.out.println("Var2 is: "+var2); } public static void main(String args[]) { disp(); } } ``` **输出:** ```java Var1 is: 0 Var2 is: null ``` ### 示例 2:静态变量在类的所有实例之间共享 在此示例中,字符串变量是非静态的,整数变量是静态的。正如您在输出中看到的那样,两个对象的非静态变量是不同的,但静态变量在它们之间共享,这就是对象`ob2`对静态变量所做的更改在两个对象中反映的原因。 ```java class JavaExample{ //Static integer variable static int var1=77; //non-static string variable String var2; public static void main(String args[]) { JavaExample ob1 = new JavaExample(); JavaExample ob2 = new JavaExample(); /* static variables can be accessed directly without * any instances. Just to demonstrate that static variables * are shared, I am accessing them using objects so that * we can check that the changes made to static variables * by one object, reflects when we access them using other * objects */ //Assigning the value to static variable using object ob1 ob1.var1=88; ob1.var2="I'm Object1"; /* This will overwrite the value of var1 because var1 has a single * copy shared among both the objects. */ ob2.var1=99; ob2.var2="I'm Object2"; System.out.println("ob1 integer:"+ob1.var1); System.out.println("ob1 String:"+ob1.var2); System.out.println("ob2 integer:"+ob2.var1); System.out.println("ob2 STring:"+ob2.var2); } } ``` 输出: ```java ob1 integer:99 ob1 String:I'm Object1 ob2 integer:99 ob2 STring:I'm Object2 ``` 有关参考的更多详细信息: [Java - 静态变量](https://beginnersbook.com/2013/05/static-variable/) ## Java 静态方法 静态方法可以在不使用类的对象(实例)的情况下访问类变量(静态变量),但是只能使用对象访问非静态方法和非静态变量。 可以在静态和非静态方法中直接访问静态方法。 **语法:** 静态关键字后跟返回类型,后跟方法名称。 ```java static return_type method_name(); ``` ### 示例 1:静态方法`main`访问没有对象的静态变量 ```java class JavaExample{ static int i = 10; static String s = "Beginnersbook"; //This is a static method public static void main(String args[]) { System.out.println("i:"+i); System.out.println("s:"+s); } } ``` **输出:** ```java i:10 s:Beginnersbook ``` ### 示例 2:直接在静态和非静态方法中访问的静态方法 ```java class JavaExample{ static int i = 100; static String s = "Beginnersbook"; //Static method static void display() { System.out.println("i:"+i); System.out.println("i:"+s); } //non-static method void funcn() { //Static method called in non-static method display(); } //static method public static void main(String args[]) { JavaExample obj = new JavaExample(); //You need to have object to call this non-static method obj.funcn(); //Static method called in another static method display(); } } ``` 输出: ```java i:100 i:Beginnersbook i:100 i:Beginnersbook ``` 阅读更多:[静态方法与 Java 中的非静态方法](https://beginnersbook.com/2013/05/static-vs-non-static-methods/) ## 静态类 只有当它是嵌套类时,才能使**静态**成为类。 1. 嵌套的静态类不需要引用外类 2. 静态类无法访问外部类的非静态成员 我们将在一个例子的帮助下看到这两点: ### 静态类示例 ```java class JavaExample{ private static String str = "BeginnersBook"; //Static class static class MyNestedClass{ //non-static method public void disp() { /* If you make the str variable of outer class * non-static then you will get compilation error * because: a nested static class cannot access non- * static members of the outer class. */ System.out.println(str); } } public static void main(String args[]) { /* To create instance of nested class we didn't need the outer * class instance but for a regular nested class you would need * to create an instance of outer class first */ JavaExample.MyNestedClass obj = new JavaExample.MyNestedClass(); obj.disp(); } } ``` 输出: ```java BeginnersBook ```