📅  最后修改于: 2020-09-24 00:38:15             🧑  作者: Mango
Java中的static关键字主要用于内存管理。我们可以将static关键字与变量,方法,块和嵌套类一起使用。static关键字属于该类,而不是该类的实例。
静态可以是:
如果将任何变量声明为静态变量,则称为静态变量。
它使您的程序内存高效(即节省内存)。
class Student{
int rollno;
String name;
String college="ITS";
}
假设我的大学有500名学生,那么每次创建对象时,所有实例数据成员都将获得内存。所有学生都有其唯一的rollno和名称,因此在这种情况下实例数据成员是很好的。在这里,“学院”是指所有对象的共同属性。如果我们将其设为静态,则此字段将仅获得一次内存。
//Java Program to demonstrate the use of static variable
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 objects
public 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();
}
}
输出:
在此示例中,我们创建了一个名为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 created
Counter(){
count++;//incrementing value
System.out.println(count);
}
public static void main(String args[]){
//Creating objects
Counter 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 value
Counter2(){
count++;//incrementing the value of static variable
System.out.println(count);
}
public static void main(String args[]){
//creating objects
Counter2 c1=new Counter2();
Counter2 c2=new Counter2();
Counter2 c3=new Counter2();
}
}
输出:
如果以任何方法应用static关键字,则称为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 object
public 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();
}
}
//Java Program to get the cube of a given number using the static method
class 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);
}
}
静态方法有两个主要限制。他们是:
class A{
int a=40;//non static
public static void main(String args[]){
System.out.println(a);
}
}
回答)这是因为不需要调用该对象的静态方法。如果它是非静态方法,则JVM首先创建一个对象,然后调用main()方法,这将导致额外的内存分配问题。
class A2{
static{System.out.println("static block is invoked");}
public static void main(String args[]){
System.out.println("Hello main");
}
}
回答)不,其中一种方法是使用静态块,但是直到JDK1.6才有可能。从JDK1.7开始,没有main方法就无法执行Java类。
class A3{
static{
System.out.println("static block is invoked");
System.exit(0);
}
}
输出:
从JDK1.7及更高版本开始,输出将是: