Java中的static关键字示例
在本教程中,我们将在中看到关于static关键字java.So 静态关键字可以与以下项关联:
- 变量
- 方法
- 代码块
- 嵌套类
静态变量:
如果任何实例变量被声明为静态。
它被称为静态变量。
static int countryCounter;
关于静态变量的一些点是:
- 静态变量属于不对对象的类。
- 静态变量在课堂加载时仅在类区域中初始化一次
- 所有对象共享静态变量的单副本
- 我们无需创建对象以访问静态变量。我们可以使用类名直接访问它。
- 静态变量也可以通过实例访问Java方法访问。
如果我们创建一个具有两个变量的类,则该类的一个静态,一个非静态。
该类的静态变量(从该类创建的对象)获取其自己的该变量版本。
但是通过静态变量,它属于程序,只有一个。
虽然来自该类的任何对象都可以引用它。
示例:让我们采取一个非常简单的例子。
我们想追踪我们创建的对象数量。
Class.lets中有一个静态变量表示程序是:Country.java
package org.igi.theitroad;
/*
 * @author:igi Mandliya
 */
public class Country {
 
    //static variable
   static int countryCounter;
  
    //instance variable
    String name;
    int dummyCounter;
    Country(String name)
    {
        this.name=name;
        countryCounter++;
        
        dummyCounter++;
    }
  
    public static void main(String[] args)
    {
        System.out.println("");
 
        Country Netherlands=new Country("Netherlands");
        System.out.println("Country Name: "+Netherlands.getName());
        System.out.println("Number of country object created: "+Country.countryCounter);
        System.out.println("Dummy counter not a static variable: "+Netherlands.dummyCounter);
        System.out.println("");
        Country france=new Country("France");
        System.out.println("Country Name: "+france.getName());
        System.out.println("Number of country object created: "+france.countryCounter);
        System.out.println("Dummy counter not a static variable: "+france.dummyCounter);
 
        System.out.println("");
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
 
}
输出:当我们运行上面的程序时,我们将获取以下结果:
Country Name: Netherlands Number of country object created: 1 Dummy counter not a static variable 1 Country Name: France Number of country object created: 2 Dummy counter not a static variable 1
在上面的示例中,我们有一个名为"countrycounter"的静态变量,因此每当我们创建一个新对象时,它的值将递增1,因为所有国家对象都是共享的逆向变量,我们只要每当名为"dummycounter"的实例变量创建任何新对象,它的值再次初始化为0。
静态方法:
如果任何方法被声明为静态。
它被称为静态方法。
public static void printCountryCounter()
    {
        System.out.println("Number of country object created: "+countryCounter);
    }
关于静态方法的一些要点是:
- 静态方法属于不是对象的类。
- 可以使用ClassName直接调用静态方法。虽然它也可以使用ObjectName调用它
- 通常静态方法用于获取静态字段。
- 我们可以通过Java实例方法访问静态方法。
- 我们无法以静态方法访问非静态方法或者实例变量。
- 静态方法不能引用此或者超级关键字。
示例:我们将创建上面的示例,并将使用一个静态方法PrintCountryCounter用于打印CountryCounter变量。
Country.java.
package org.igi.theitroad;
/*
 * @author:igi Mandliya
 */
public class Country {
 
    String name;
    static int countryCounter;
    int dummyCounter;
    Country(String name)
    {
        this.name=name;
        countryCounter++;
       
        dummyCounter++;
    }
    
 
    public static void main(String[] args)
    {
        Country Netherlands=new Country("Netherlands");
       
        System.out.println("");
        System.out.println("Country Name: "+Netherlands.getName());
        printCountryCounter();
        System.out.println("Dummy counter not a static variable: "+Netherlands.dummyCounter);
        System.out.println("");
        Country france=new Country("France");
        System.out.println("Country Name: "+france.getName());
        printCountryCounter();
        System.out.println("Dummy counter not a static variable: "+france.dummyCounter);
        System.out.println("");
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
    
    public static void printCountryCounter()
    {
        System.out.println("Number of country object created: "+countryCounter);
    }
}
输出:当我们运行上面的程序时,我们将获取以下结果:
Country Name: Netherlands Number of country object created: 1 Dummy counter not a static variable 1 Country Name: France Number of country object created: 2 Dummy counter not a static variable 1
为什么我们无法从静态方法访问非静态成员:
从静态方法调用非静态方法时,我们将获得编译错误。
例如:在上面的printcountrycounter()中,让我们说你调用给getName()你会编译错误。
public static void printCountryCounter()
 
 {
 
        getName();  //Compilation error
 
        System.out.println("Number of country object created: "+countryCounter);
 
 }
想象一下,当你被允许从printcountrycounter()调用getName()时。
我们可以使用类名称调用printcountrycounter(),所以当我们调用printcountrycounter()时,GetName()应该返回的值,因为值可能不同新对象,它将引用哪个值。
因此,这就是为什么,不允许从静态方法调用非静态成员。
静态块:
静态块,是Java类内的一个语句,当一个类首先加载到JVM时将被执行
关于静态块的一些点是:
- 静态块用于初始化静态数据成员。
- 静态块在主方法执行之前执行。
package org.igi.theitroad;
public class StaticBlockExample {
 
    /**
 
     * @Author:igi Mandliya
 
     */
 
    static{
        System.out.println("This block will get call before main method");
    }
 
    public static void main(String[] args) {
        
        System.out.println("In main method");
    }
}
输出:当我们运行上面的程序时,我们将获取以下结果:
This block will get call before main method In main method
静态类:
在Java中,我们可以在另一个类中定义一个类。
这样的类被称为嵌套类。
封闭嵌套类的类被称为外部类。
在Java中,我们无法制作外层静态。
只能声明为静态的嵌套类
关于嵌套静态类的一些点数是:
- 嵌套的静态类不需要外表的参考。
- 静态类无法访问外部类的非静态成员。它只能访问外部类的静态成员,包括私有。
例子:
package org.igi.theitroad;
/*
 * @author:igi Mandliya
 */
public class OuterClass {
 
 
    static class InnerStaticClass{
       
        public void printInnerClass()
        {
            System.out.println("In inner class");
        }
    }
       
    public static void main(String[] args)
    {
       
       
        System.out.println("");
        OuterClass.InnerStaticClass inc=new OuterClass.InnerStaticClass();
        inc.printInnerClass();
        System.out.println("");
    }
 
    public static void printOuterClass()
    {
        System.out.println("In outer class");
    }
    
}
输出:当我们运行上面的程序时,我们将获取以下结果:
In inner class

