Java 靜態方法、類、變量和塊的基礎知識
已發表: 2019-12-13
Java中的靜態是什麼?
- static 關鍵字可以與類、變量、方法和塊一起使用。
- 靜態成員不屬於任何特定實例。
- 靜態成員僅屬於該類。
- 將成員設為靜態後,您可以在沒有任何對象的情況下訪問它。
您有以下任何問題嗎?
- 你能寫下 Java 靜態方法的最佳實踐嗎?
- 什麼是接口中的 Java 靜態方法?
- 對 Java 靜態方法與單例有疑問嗎?
- Java 靜態方法與實例方法的性能
- Java 靜態方法和變量
- Java 靜態方法與非靜態方法
- 抽像類中的 Java 靜態方法
static 關鍵字可用於 3 種場景:
- 靜態變量
- 靜態方法
- 靜態代碼塊。
靜態變量(“靜態”關鍵字 = 類變量)

在 Java 中,可以使用“ static
”關鍵字聲明變量。
示例: static int y = 0;
當使用關鍵字static
聲明變量時,它被稱為class variable
。 所有實例共享同一個變量副本。 類變量可以直接用類訪問,而不需要創建實例。
沒有“靜態”關鍵字 = 實例變量
沒有static keyword
,它被稱為instance variable
,並且類的每個實例都有自己的變量副本。
示例:靜態 int crunchify_variable_name
;
- 靜態變量在類的所有實例之間共享。
- 當您想要進行大量內存管理時,您需要它的主要原因之一。
- 對於所有靜態變量——只有一個副本可供您使用。
- 您絕對不需要類對象來訪問靜態變量。
- 直接用就行了。 你不需要
object.StaticVariable
- 直接用就行了。 你不需要
什麼是靜態塊?
靜態塊是Java class
中的語句塊,將在類首次加載到 JVM 時執行。
你可以覆蓋Java中的私有方法嗎?
- 嗯,不。 私有方法不能被覆蓋,因為它不能在類外使用。
為什麼我們不能覆蓋 Java 中的靜態方法?
- 靜態方法也不能被覆蓋,因為它是類而不是對象的一部分
我們可以在靜態上下文中訪問非靜態變量嗎?
- 為了能夠從您的靜態方法訪問非靜態變量,它們需要是靜態成員變量。
靜態方法的詳細信息
- 如何識別? 只需先檢查一下。 你需要一個類對象來訪問靜態方法嗎? 如果你不需要一個對象,那麼它就是靜態方法。
- 您需要使用 static 關鍵字來指定靜態方法
- 如果該方法在運行時不會在整個項目中發生變化,則最好使用關鍵字 static。
- 您不能覆蓋靜態方法。
我們來看看下面的例子:
示例 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package crunchify . com . tutorials ; /** * @author Crunchify.com * */ public class CrunchifyStaticMethodMain { public static void main ( String args [ ] ) { String [ ] crunchifyObject = new String [ 3 ] ; crunchifyObject = new String [ ] { "Google" , "Facebook" , "Crunchify" } ; // creating instnace CrunchifyStaticMethodMain object = new CrunchifyStaticMethodMain ( ) ; object . crunchifyTestMethod ( crunchifyObject ) ; } /* * Check this out: Let's understand little more... * * Here method crunchifyTestMethod is defined as * public void crunchifyTestMethod(String[]) * so it is "non-static". It can't be called unless it is called on an instance of CrunchifyStaticMethodMain. * * If you declared your method as * public static void crunchifyTestMethod(int[]) * then you could call: CrunchifyStaticMethodMain.crunchifyTestMethod(arr); within main without having created an instance for it. */ public void crunchifyTestMethod ( String [ ] crunchifyObject ) { for ( int i = 0 ; i < crunchifyObject . length ; i ++ ) System . out . println ( crunchifyObject [ i ] ) ; } } |
查看above code block
中的解釋。 我們必須創建 Class 的實例來訪問非靜態方法。
示例 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package com . crunchify . tutorials ; /** * @author Crunchify.com */ public class CrunchifyStaticDeclaration { // 1st static block static { System . out . println ( "\nI'm static block 1.." ) ; setTestString ( "This is static block's String" ) ; setTestValue ( 2 ) ; } // 2nd static blocks in same class static { System . out . println ( "\nI'm static block 2.." ) ; } // static variable example private static int testValue ; // kept private to control it's value through setter public int getTestValue ( ) { return testValue ; } // static method example public static void setTestValue ( int testValue ) { if ( testValue > 0 ) CrunchifyStaticDeclaration . testValue = testValue ; System . out . println ( "setTestValue method: " + testValue ) ; } public static String testString ; /** * @return the testString */ public static String getTestString ( ) { return testString ; } /** * @param testString the testString to set */ public static void setTestString ( String testString ) { CrunchifyStaticDeclaration . testString = testString ; System . out . println ( "setTestString method: " + testString ) ; } // static util method public static int subValue ( int i , int . . . js ) { int sum = i ; for ( int x : js ) sum -= x ; return sum ; } } |
現在讓我們進行測試:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com . crunchify . tutorials ; /** * @author Crunchify.com */ public class CrunchifyStaticExample { public static void main ( String [ ] args ) { CrunchifyStaticDeclaration . setTestValue ( 5 ) ; // non-private static variables can be accessed with class name CrunchifyStaticDeclaration . testString = "\nAssigning testString a value" ; CrunchifyStaticDeclaration csd = new CrunchifyStaticDeclaration ( ) ; System . out . println ( csd . getTestString ( ) ) ; // class and instance static variables are same System . out . print ( "\nCheck if Class and Instance Static Variables are same: " ) ; System . out . println ( CrunchifyStaticDeclaration . testString == csd . testString ) ; System . out . println ( "Why? Because: CrunchifyStaticDeclaration.testString == csd.testString" ) ; } } |
輸出:
1 2 3 4 5 6 7 8 9 10 11 |
I 'm static block 1.. setTestString method: This is static block' s String setTestValue method : 2 I ' m static block 2.. setTestValue method : 5 Assigning testString a value Check if Class and Instance Static Variables are same : true Why ? Because : CrunchifyStaticDeclaration . testString == csd . testString |
我希望你真的能得到關於靜態方法、變量和塊的好信息。

如果您在運行以上程序時遇到任何問題或在理解 Java 中的靜態關鍵字時遇到問題,請告訴我們。