Java Access Modifiers – public, protected, private and default筆記

1 . Java static variables: We can use static keyword with a class level variable. A static variable is a class variable and doesn’t belong to Object/instance of the class. Since static variables are shared across all the instances of Object, they are not thread safe. Usually static variables are used with final keyword for common resources or constants that can be used by all the objects. If the static variable is not private, we can access it with ClassName.variableName

//static variable example
private static int count;
public static String str;
public static final String DB_USER = "myuser";

2 . Java static methods: Same as static variables, static methods belong to class and not to class instances. A static method can access only static variables of class and invoke only static methods of the class. Usually static methods are utility methods that we want to expose to be used by other classes without the need of creating an instance; for example Collections class. Java Wrapper classes and utility classes contains a lot of static methods. The main() method that is the entry point of a java program itself is a static method.

//static method example
public static void setCount(int count) {
    if(count > 0)
    StaticExample.count = count;
}
 
//static util method
public static int addInts(int i, int...js){
    int sum=i;
    for(int x : js) sum+=x;
    return sum;
}

From Java 8 onwards, we can have static methods in interfaces too, for more details please readJava interface static methods.

3 .Java static Block: Java static block is the group of statements that gets executed when the class is loaded into memory by Java ClassLoader. It is used to initialize static variables of the class. Mostly it’s used to create static resources when class is loaded. We can’t access non-static variables in static block. We can have multiple static blocks in a class, although it doesn’t make much sense. Static block code is executed only once when class is loaded into memory.

static{
    //can be used to initialize resources when class is loaded
    System.out.println("StaticExample static block");
    //can access only static variables and methods
    str="Test";
    setCount(2);
}

4 . Java Static Class: We can use static keyword with nested classes. static keyword can’t be used with top-level classes. Static nested class is same as any other top-level class and is nested for only packaging convenience.Read: Java Nested Classes

摘自:http://www.journaldev.com/7153/java-101-tutorials-learn-java

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末辱姨,一起剝皮案震驚了整個(gè)濱河市师抄,隨后出現(xiàn)的幾起案子最铁,更是在濱河造成了極大的恐慌折联,老刑警劉巖官脓,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件仿贬,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡品抽,警方通過(guò)查閱死者的電腦和手機(jī)储笑,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)圆恤,“玉大人突倍,你說(shuō)我怎么就攤上這事∨桕迹” “怎么了赘方?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)弱左。 經(jīng)常有香客問(wèn)我,道長(zhǎng)炕淮,這世上最難降的妖魔是什么拆火? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮涂圆,結(jié)果婚禮上们镜,老公的妹妹穿的比我還像新娘。我一直安慰自己润歉,他們只是感情好模狭,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著踩衩,像睡著了一般嚼鹉。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上驱富,一...
    開(kāi)封第一講書(shū)人閱讀 51,301評(píng)論 1 301
  • 那天锚赤,我揣著相機(jī)與錄音,去河邊找鬼褐鸥。 笑死线脚,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播浑侥,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼姊舵,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了寓落?” 一聲冷哼從身側(cè)響起括丁,我...
    開(kāi)封第一講書(shū)人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎零如,沒(méi)想到半個(gè)月后躏将,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡考蕾,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年祸憋,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片肖卧。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蚯窥,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出塞帐,到底是詐尸還是另有隱情拦赠,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布葵姥,位于F島的核電站荷鼠,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏榔幸。R本人自食惡果不足惜允乐,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望削咆。 院中可真熱鬧牍疏,春花似錦、人聲如沸拨齐。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)瞻惋。三九已至厦滤,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間歼狼,已是汗流浹背馁害。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蹂匹,地道東北人碘菜。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親忍啸。 傳聞我的和親對(duì)象是個(gè)殘疾皇子仰坦,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容