static关键字可用于类,变量,方法和块,static成员属于类,而不是特定的实例,这意味着如果创建成员static,则可以不用实例化就可以访问它。
静态成员对于该类的所有实例(对象)是公共的,但非静态成员对于该类的每个实例都是独立的。
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();
}
}
static块
例子1:单个static块
可以看到,在main方法中访问static变量之前,它们都被初始化了。
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);
}
}
输出:Value of num: 97
Value of mystr: Static keyword in Java
例子2:多个static块
看看Java中的多个static块是如何工作的,它们按给定的顺序执行,这意味着第一个static块在第二个static块之前执行,因此,第一个块初始化的值被第二个块覆盖。
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);
}
}
输出:Static Block 1
Static Block 2
Value of num: 98
Value of mystr: Block2
Java static变量
static变量是类的所有实例(或对象)的公共变量,因为它是一个类级别变量,换句话说,在类的所有实例中只创建并共享一个static变量的副本,此类变量的内存分配只在类加载到内存时发生一次,一些要点:static变量也称为类变量
与非静态变量不同,可以直接在静态和非静态方法中访问此类变量。
例子1:static变量可以直接在static方法中访问
这里有一个static方法disp()和两个static变量,在static方法中直接访问这两个变量。
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();
}
}
输出:Var1 is: 0
Var2 is: null
例子2:static变量在类的所有实例中共享
在此示例中,String变量为非静态,而integer变量为Static。
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);
}
}
输出:ob1 integer:99
ob1 String:I'm Object1
ob2 integer:99
ob2 STring:I'm Object2
Java static方法
静态方法可以访问类变量(静态变量),而无需使用类的对象(实例),但是非静态方法和非静态变量只能使用对象进行访问。静态方法可以直接在静态和非静态方法中访问。static return_type method_name();
示例1:静态方法主要是在没有对象的情况下访问静态变量
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);
}
}
输出:i:10
s:Beginnersbook
示例2:直接在静态和非静态方法中访问的静态方法
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();
}
}
输出:i:100
i:Beginnersbook
i:100
i:Beginnersbook
static类
仅当一个类是嵌套类时,才可以将其设为静态。
static类示例
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();
}
}
输出: