Java 允許在一個類中定義另一個類。這些被稱為嵌套類。大多數開發人員都知道類可以是靜態的,因此在 Java 中可以將某些類設為靜態。 Java支持靜態實例變量,靜態方法,靜態塊和靜態類。定義嵌套類的類稱為外層類。與頂級課程不同,嵌套類可以是靜態的。非靜態嵌套類也稱為內部類.
Note: The top level class cannot be static in java, to create a static class we must create a nested class and then make it static.
如果沒有外部類的實例,則無法創建內部類的實例。因此,內部類實例可以訪問其外部類的所有成員,而無需使用外部類實例的引用。因此,內部類可以幫助使程序變得簡單和簡潔。
Remember: In static class, we can easily create objects.
靜態和非靜態嵌套類的區別
以下是靜態嵌套類和內部類之間的主要區別。
- 靜態嵌套類可以在不實例化其外部類的情況下實例化。
- 內部類可以訪問外部類的靜態和非靜態成員。靜態類隻能訪問外部類的靜態成員。
示例的靜態和非靜態嵌套類
下麵是上麵提到的topic的實現:
Java
// Java program to Demonstrate How to
// Implement Static and Non-static Classes
// Class 1
// Helper class
class OuterClass {
// Input string
private static String msg = "GeeksForGeeks";
// Static nested class
public static class NestedStaticClass {
// Only static members of Outer class
// is directly accessible in nested
// static class
public void printMessage()
{
// Try making 'message' a non-static
// variable, there will be compiler error
System.out.println(
"Message from nested static class: " + msg);
}
}
// Non-static nested class -
// also called Inner class
public class InnerClass {
// Both static and non-static members
// of Outer class are accessible in
// this Inner class
public void display()
{
// Print statement whenever this method is
// called
System.out.println(
"Message from non-static nested class: "
+ msg);
}
}
}
// Class 2
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating instance of nested Static class
// inside main() method
OuterClass.NestedStaticClass printer
= new OuterClass.NestedStaticClass();
// Calling non-static method of nested
// static class
printer.printMessage();
// Note: In order to create instance of Inner class
// we need an Outer class instance
// Creating Outer class instance for creating
// non-static nested class
OuterClass outer = new OuterClass();
OuterClass.InnerClass inner
= outer.new InnerClass();
// Calling non-static method of Inner class
inner.display();
// We can also combine above steps in one
// step to create instance of Inner class
OuterClass.InnerClass innerObject
= new OuterClass().new InnerClass();
// Similarly calling inner class defined method
innerObject.display();
}
}
輸出
Message from nested static class: GeeksForGeeks Message from non-static nested class: GeeksForGeeks Message from non-static nested class: GeeksForGeeks
相關用法
- Java Static Variables用法及代碼示例
- Java StackTraceElement.getClassName()用法及代碼示例
- Java StackTraceElement.getFileName()用法及代碼示例
- Java StackTraceElement.getLineNumber()用法及代碼示例
- Java StackTraceElement.getMethodName()用法及代碼示例
- Java StackTraceElement.isNativeMethod()用法及代碼示例
- Java StackTraceElement equals()用法及代碼示例
- Java StackTraceElement getClassName()用法及代碼示例
- Java StackTraceElement hashCode()用法及代碼示例
- Java StackTraceElement isNativeMethod()用法及代碼示例
- Java StackTraceElement toString()用法及代碼示例
- Java Stack addAll(Collection)用法及代碼示例
- Java Stack addElement(E)用法及代碼示例
- Java Stack add(Object)用法及代碼示例
- Java Stack capacity()用法及代碼示例
- Java Stack clear()用法及代碼示例
- Java Stack clone()用法及代碼示例
- Java Stack contains()用法及代碼示例
- Java Stack containsAll()用法及代碼示例
- Java Stack copyInto()用法及代碼示例
- Java Stack elementAt()用法及代碼示例
- Java Stack elements()用法及代碼示例
- Java Stack empty()用法及代碼示例
- Java Stack ensureCapacity()用法及代碼示例
- Java Stack equals()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Static class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。