Java static关键字
Java 中的
static关键字主要用于内存管理。我们可以对变量,方法,块和嵌套类应用静态关键字。静态关键字比该类的实例属于该类。
静态关键字可以是:
变量(也称为类变量)
方法(也称为类方法)
代码块
嵌套类
1)Java static变量
如果将任何变量声明为静态,则称为静态变量。
静态变量可用于引用所有对象的公共属性(每个对象不是唯一的),例如,员工的公司名称,学生的大学名称等。
静态变量在类加载时仅在类区域中获得一次内存。
静态变量的优点
它使您的程序
高效存储(即,它可以节省内存)。
了解没有静态变量的问题
class Student{
int rollno;
String name;
String college="ITS";
}
假设我的大学有500名学生,现在每次创建对象时,所有实例数据成员都将获得内存。所有学生都有其唯一的rollno和名称,因此在这种情况下实例数据成员是很好的。在此,"学院"是指所有对象的共有属性。如果我们将其设为静态,则该字段将仅获得一次内存。
注意: Java静态属性已共享给所有对象。
静态变量的示例
class Student{
int rollno;
//instance variable String name;
static String college ="ITS";
//static variable
//constructor Student(int r, String n){
rollno = r;
name = n;
}
//method to display the values void display (){
System.out.println(rollno+" "+name+" "+college);
}
}
//Test class to show the values of objectspublic class TestStaticVariable1{
public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
//we can change the college of all objects by the single line of code
//Student.college="BBDIT";
s1.display();
s2.display();
}
}
输出:
111 Karan ITS222 Aryan ITS
没有静态变量的计数器程序
在此示例中,我们创建了一个名为count的实例变量在构造函数中增加。由于实例变量在创建对象时获取内存,因此每个对象都将具有实例变量的副本。如果增加,它将不会反映其他对象。因此,每个对象在count变量中的值为1。
//Java Program to demonstrate the use of an instance variable
//which get memory each time when we create an object of the class.class Counter{
int count=0;
//will get memory each time when the instance is createdCounter(){
count++;
//incrementing valueSystem.out.println(count);
}
public static void main(String args[]){
//Creating objectsCounter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
输出:
使用静态变量的计数器程序
如上所述,静态变量只会获得一次内存,如果任何对象更改了静态变量的值,它将保留其值。
//Java Program to illustrate the use of static variable which
//is shared with all objects.class Counter2{
static int count=0;
//will get memory only once and retain its valueCounter2(){
count++;
//incrementing the value of static variableSystem.out.println(count);
}
public static void main(String args[]){
//creating objectsCounter2 c1=new Counter2();
Counter2 c2=new Counter2();
Counter2 c3=new Counter2();
}
}
输出:
2)Java静态方法
如果通过任何方法应用static关键字,则称为静态方法
静态方法属于类而不是类的对象。
可以调用静态方法,而无需创建类的实例。
静态方法可以访问静态数据成员并可以更改其值。
静态方法的示例
//Java Program to demonstrate the use of a static method.class Student{
int rollno;
String name;
static String college = "ITS";
//static method to change the value of static variable static void change(){
college = "BBDIT";
}
//constructor to initialize the variable Student(int r, String n){
rollno = r;
name = n;
}
//method to display values void display(){
System.out.println(rollno+" "+name+" "+college);
}
}
//Test class to create and display the values of objectpublic class TestStaticMethod{
public static void main(String args[]){
Student.change();
//calling change method
//creating objects Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
Student s3 = new Student(333,"Sonoo");
//calling display method s1.display();
s2.display();
s3.display();
}
}
Output:111 Karan BBDIT 222 Aryan BBDIT 333 Sonoo BBDIT
执行常规计算的静态方法的另一个示例
//Java Program to get the cube of a given number using the static methodclass Calculate{
static int cube(int x){
return x*x*x;
}
public static void main(String args[]){
int result=Calculate.cube(5);
System.out.println(result);
}
}
静态方法的限制
静态方法的主要限制有两个。他们是:
静态方法不能使用非静态数据成员或直接调用非静态方法。
this和super不能在静态上下文中使用。
class A{
int a=40;
//non static public static void main(String args[]){
System.out.println(a);
}
}
Output:Compile Time Error
Q)为什么Java main方法是静态的?
Ans)这是因为不需要该对象调用静态方法。如果它是非静态方法,则JVM首先创建一个对象,然后调用main()方法,这将导致额外的内存分配问题。
3)Java静态块
用于初始化静态数据成员。
在类加载时在main方法之前执行。
静态块示例
class A2{
static{
System.out.println("static block is invoked");
}
public static void main(String args[]){
System.out.println("Hello main");
}
}
Output:static block is invoked Hello main
Q)我们可以执行没有main()方法的程序吗?
Ans)不,其中之一方式是静态块,但直到JDK 1.6才有可能。从JDK 1.7开始,没有main方法,就无法执行Java类。
class A3{
static{
System.out.println("static block is invoked");
System.exit(0);
}
}
输出:
从JDK 1.7及更高版本开始,输出将为:
Error: Main method not found in class A3, please define the main method as: public static void main(String[] args)or a JavaFX application class must extend javafx.application.Application