📜  Java – 最终与静态访问修饰符

📅  最后修改于: 2022-05-13 01:55:13.763000             🧑  作者: Mango

Java – 最终与静态访问修饰符

final 关键字在不同的上下文中使用。首先,final 是一个非访问修饰符,仅适用于变量、方法或类。以下是使用 final 的不同上下文。而Java中的static关键字主要用于内存管理。 Java中的 static 关键字用于共享给定类的相同变量或方法。用户可以将静态关键字应用于变量、方法、块和嵌套类。

最终访问修饰符

final 访问修饰符是适用于类、方法和变量的修饰符。如果我们将父类方法声明为final,那么我们不能在子类中覆盖该方法,因为它的实现是final,如果一个类被声明为final,我们就不能扩展该类的功能,即我们不能创建该类的子类,即最终类不能继承。 final 类中存在的每个方法始终是 final y 默认值,但 final 类中存在的每个变量都不必是 final。 final 关键字的主要优点是我们可以实现安全性,并且可以提供独特的实现。但是 final 关键字的主要缺点是我们缺少 OOP 的关键好处,例如继承(因为 final 类)、多态性(因为 final 方法),因此如果没有特定要求,则不建议使用 final关键词。

示例 1:

Java
// Java Program to illustrate Final keyword
// Where No final keyword Is Used
 
// Importing required classes
import java.io.*;
import java.util.*;
 
// Class 1
// Super-class
class P {
 
    // Method 1
    // To declare first name
    public void firstName()
    {
 
        // Passing name and print it
        System.out.println("Mayank");
    }
 
    // Method 2
    // To declare last name
    public void surName()
    {
 
        // Passing name and print it
        System.out.println("Trivedi");
    }
}
 
// Class 2
// Sub-class
// Extending above class
class C extends P {
 
    // Method 1
    // Trying to override the last name
    public void surName()
    {
        // Display surname
        System.out.println("Sharma");
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Display message
        System.out.println("GFG");
    }
}


Java
// Java Program to illustrate Final keyword
// When final keyword Is Used
 
// Importing required classes
import java.io.*;
import java.util.*;
 
// Class 1
// Super-class
class P {
 
    // Method 1
    // To declare first name
    public void firstName()
    {
 
        // Passing name and print it
        System.out.println("Mayank");
    }
 
    // Method 2
    // To declare last name
    public final void surName()
    {
 
        // Passing name and print it
        System.out.println("Trivedi");
    }
}
 
// Class 2
// Sub-class
// Extending above class
class C extends P {
 
    // Method 1
    // Trying to override the last name
    public void surName()
    {
        // Display surname
        System.out.println("Sharma");
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Display message
        System.out.println("GFG");
    }
}


Java
// Java Program to Illustrate Static Access Modifier
 
// Importing required classes
import java.io.*;
import java.util.*;
 
// Main class
class GFG {
 
    // Creating a static variable and
    // initializing a custom value
    static int x = 10;
 
    // Creating a instance variable and
    // initializing a custom value
    int y = 20;
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of class inside main() method
        GFG t1 = new GFG();
 
        // Accessing and re-initializing the
        // static and instance variable
        // using t1 reference
        t1.x = 88;
        t1.y = 99;
 
        // Creating an object of class inside main() method
        // again
        GFG t2 = new GFG();
 
        // Accessing the static and instance variable using
        // t2 reference as we know that for each object
        // there is a separate copy of instance variable
        // created. While a same copy of static variable will
        // be shared between the objects
 
        // Displaying the value of static and instance
        // variable using t2 object reference
        System.out.println(
            "Value of Static variable x = " + t2.x + "\n"
            + "Value of Instance variable y = " + t2.y);
    }
}



输出
GFG

示例2:

Java

// Java Program to illustrate Final keyword
// When final keyword Is Used
 
// Importing required classes
import java.io.*;
import java.util.*;
 
// Class 1
// Super-class
class P {
 
    // Method 1
    // To declare first name
    public void firstName()
    {
 
        // Passing name and print it
        System.out.println("Mayank");
    }
 
    // Method 2
    // To declare last name
    public final void surName()
    {
 
        // Passing name and print it
        System.out.println("Trivedi");
    }
}
 
// Class 2
// Sub-class
// Extending above class
class C extends P {
 
    // Method 1
    // Trying to override the last name
    public void surName()
    {
        // Display surname
        System.out.println("Sharma");
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Display message
        System.out.println("GFG");
    }
}


输出:

静态访问修饰符

静态访问修饰符是适用于方法和变量但不适用于类的访问修饰符。我们可以使用 static 修饰符声明顶级类,但我们可以将内部类声明为静态(这种类型的内部类称为静态嵌套类)。在每个对象的实例变量的情况下,将创建一个单独的副本,但在静态变量的情况下,将在类级别创建一个副本并由该类的每个对象共享。

例子

Java

// Java Program to Illustrate Static Access Modifier
 
// Importing required classes
import java.io.*;
import java.util.*;
 
// Main class
class GFG {
 
    // Creating a static variable and
    // initializing a custom value
    static int x = 10;
 
    // Creating a instance variable and
    // initializing a custom value
    int y = 20;
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of class inside main() method
        GFG t1 = new GFG();
 
        // Accessing and re-initializing the
        // static and instance variable
        // using t1 reference
        t1.x = 88;
        t1.y = 99;
 
        // Creating an object of class inside main() method
        // again
        GFG t2 = new GFG();
 
        // Accessing the static and instance variable using
        // t2 reference as we know that for each object
        // there is a separate copy of instance variable
        // created. While a same copy of static variable will
        // be shared between the objects
 
        // Displaying the value of static and instance
        // variable using t2 object reference
        System.out.println(
            "Value of Static variable x = " + t2.x + "\n"
            + "Value of Instance variable y = " + t2.y);
    }
}
输出
Value of Static variable x = 88
Value of Instance variable y = 20

在对上述说明符和这些重要的特殊关键字有一个很好的理解和实现之后,让我们将它们之间的差异列表化,以更好地理解这些关键字。

                     Final Access Modifier                               Static Access Modifier
This modifier is applicable to both outer and inner classes, variables, methods, and blocks.This modifier is only applicable to inner classes, methods, and variables. 
It is not necessary to initialize the final variable at the time of its declaration.It is necessary to initialize the static variable at the time of its declaration.
Final variable cannot be reinitialized.Static variables can be reinitialized.
Final method can’t be inherited.Static methods can only access the static members of the class and can only be called by other static methods.
Final class can’t be inherited by any class.The static class object can’t be created and it only contains static members only.
Final keyword doesn’t support any block for initialization of final variables.Static block is used to initialize the static variables.