📜  Java静态和非静态嵌套类的区别

📅  最后修改于: 2021-09-11 03:51:19             🧑  作者: Mango

嵌套类分为两类,即静态类和非静态类。声明为静态的嵌套类称为静态嵌套类。非静态嵌套类称为内部类。

在Java,类可以是静态的,也可以是非静态的。因此,将类设置为静态或非静态之间有很多区别。 Java有两种类,一种称为顶级类,另一种称为嵌套类。顾名思义,顶级类是在 ‘ 中声明的类。 Java’文件。

另一方面,嵌套类是在另一个类中声明的。包含嵌套类的类称为外部类。在Java编程语言中,您不能将顶级类设为静态。您只能将嵌套类设为静态或非静态。如果您将嵌套类设为非静态,则它也称为内部类。

用户案例:

  • 静态嵌套类
  • 非静态嵌套类

示例 1:静态嵌套类

Java
// Java Program to illustrate
// static nested class
 
// Importing input output classes
import java.io.*;
// Importing all classes from
// java.util package
import java.util.*;
 
// Class 1
// Helper class1
class ClassA {
 
    // Static variable
    static int x = 10;
 
    int y = 20;
 
    // Static variable with privare
    // access modifier in ClassA
    private static int z = 30;
 
    // Class 2 - Nested static class
    // Helper Class 2
    static class ClassB {
 
        // Method of nested static class
        void get()
        {
            // Execution statements whenever
            // function is called
 
            // Print and display commands
            System.out.println("x: " + x);
            System.out.println("z: " + z);
        }
    }
}
 
// Class 3
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of static nested class
        // defined outside Main class
        ClassA.ClassB ob1 = new ClassA.ClassB();
 
        // Calling the method od static nested class
        // in main() method
        ob1.get();
    }
}


Java
// Importing all input output classes
import java.io.*;
 
// Class 1
// Helper Class 1
class ClassA {
 
    // Static member variable of ClassA
    static int x = 10;
 
    int y = 20;
    public int z = 30;
 
    // Class 2 - Helper Class 2
    // Non-static nested class
    class ClassB {
 
        // Method of Non-static nested class
        void get()
        {
            // Execution command of get() method
 
            // Print and display commands
            System.out.println("x: " + x);
            System.out.println("y: " + y);
            System.out.println("z: " + z);
        }
    }
}
 
// Class 3
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of Class 1 in main() method
        ClassA ob1 = new ClassA();
 
        // Creating object of non-static nested class in
        // main()
        ClassA.ClassB ob2 = ob1.new ClassB();
 
        // Calling non-static nested class method
        // in main() method
        ob2.get();
    }
}


输出
x: 10
z: 30

示例 2:非静态嵌套类

Java

// Importing all input output classes
import java.io.*;
 
// Class 1
// Helper Class 1
class ClassA {
 
    // Static member variable of ClassA
    static int x = 10;
 
    int y = 20;
    public int z = 30;
 
    // Class 2 - Helper Class 2
    // Non-static nested class
    class ClassB {
 
        // Method of Non-static nested class
        void get()
        {
            // Execution command of get() method
 
            // Print and display commands
            System.out.println("x: " + x);
            System.out.println("y: " + y);
            System.out.println("z: " + z);
        }
    }
}
 
// Class 3
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of Class 1 in main() method
        ClassA ob1 = new ClassA();
 
        // Creating object of non-static nested class in
        // main()
        ClassA.ClassB ob2 = ob1.new ClassB();
 
        // Calling non-static nested class method
        // in main() method
        ob2.get();
    }
}

输出
x: 10
y: 20
z: 30

现在Java静态和非静态嵌套类的区别:

  • 静态内部类可以直接访问外部类的静态成员。但是,要访问外部类的实例成员,您需要实例化外部类。
  • 嵌套静态类不需要外部类的引用,但非静态嵌套类或内部类需要外部类引用。
  • 非静态嵌套类对嵌套类的成员具有完全访问权限。静态嵌套类没有对嵌套实例的引用,因此静态嵌套类不能调用非静态方法或访问嵌套类的实例的非静态字段。
  • 静态和非静态嵌套类之间的另一个区别是您不能直接访问非静态成员(例如方法和字段)到嵌套静态类中。如果你这样做,你会得到诸如“非静态成员不能在静态上下文中使用”之类的错误。而 Inner 类可以访问 Outer 类的静态和非静态成员。