Java類中this關(guān)鍵字與static關(guān)鍵字的用法解析
前言
今天給大家總結(jié)介紹一下Java類中this關(guān)鍵字和static關(guān)鍵字的用法。
this關(guān)鍵字用法:
- this.屬性可以調(diào)用類中的成員變量
- this()可以調(diào)用類中的構(gòu)造方法
1:修飾屬性,表示調(diào)用類中的成員變量。
代碼示例:
public class Student { public String name; public int age; public String school; public Student(String name, int age, String school) { this.name = name; this.age = age; this.school = school; } }
因為程序的就近匹配原則,編譯器會從調(diào)用代碼處的最近位置查找有無匹配的變量或者方法,若找到直接使用最近的變量或方法。所以如果上述代碼中的帶參構(gòu)造方法不使用this的話我們在使用該構(gòu)造方法時會遇到無法賦值的問題。
2:this修飾方法
this可用于構(gòu)造函數(shù)之間的相互調(diào)用,可以減少構(gòu)造函數(shù)代碼的耦合性,使代碼看起來更加整潔(不寫重復(fù)代碼很重要)。
未使用this前:
public class Student { public String name; public int age; public String school; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public Student(String name, int age, String school) { this.name = name; this.age = age; this.school = school; } }
使用this后:
public class Student { public String name; public int age; public String school; public Student() { } public Student(String name, int age) { this(); this.name = name; this.age = age; } public Student(String name, int age, String school) { this(name,age); this.school = school; } }
PS:
- 1.this調(diào)用構(gòu)造方法必須放在當(dāng)前構(gòu)造方法的首行調(diào)用,否則會報錯。
- 2.對構(gòu)造方法的調(diào)用不能成"環(huán)”必須線性調(diào)用,否則會陷入調(diào)用死循環(huán)。
3:this表示當(dāng)前對象的引用
當(dāng)前是通過哪個對象調(diào)用的屬性或者方法,this就指代哪一個對象。
代碼示例:
public class Student { public String name; public int age; public String school; public void show(){ System.out.println(this); } public static void main(String[] args) { Student stu1 = new Student(); stu1.show(); System.out.println(stu1); System.out.println("————————————"); Student stu2 = new Student(); stu2.show(); System.out.println(stu2); System.out.println("————————————"); Student stu3 = new Student(); stu3.show(); System.out.println(stu3); } }
輸出結(jié)果:
static關(guān)鍵字用法:
在Java的類中,若static修飾類中屬性,稱之為類的靜態(tài)屬性/類屬性,它和具體的對象無關(guān),該屬性存儲在JVM的方法區(qū)(不同于堆區(qū)和棧區(qū)的另一個區(qū)域),類中的所有對象共享同一個方法區(qū)(類中的常量和靜態(tài)變量儲存在方法區(qū)中),直接使用類名稱來訪問靜態(tài)變量,不推薦使用某個對象來訪問。
只要類一定義,JVM就會為static修飾的類屬性分配空間,它和類是綁定的,使用static修飾變量的好處是當(dāng)我們需要修改一個值時可以更加方便,比如學(xué)生類中的學(xué)校屬性,若學(xué)校改名字了,我們沒有使用static修飾,那么我們就要給每個學(xué)生丟修改一次,但是使用了static則只需要修改一次。
相關(guān)問題:Java方法中是否可以定義靜態(tài)變量?
解答:靜態(tài)變量,當(dāng)類定義時和類一塊加載到內(nèi)存中了,而調(diào)用方法至少是在類定義之后才能調(diào)用的,先后順序不一樣,就是說還沒調(diào)用方法便已經(jīng)執(zhí)行了方法里面的定義變量,這是不合理的。
到此這篇關(guān)于Java類中this關(guān)鍵字與static關(guān)鍵字的用法解析的文章就介紹到這了,更多相關(guān)Java類this關(guān)鍵字與static關(guān)鍵字內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中生成隨機(jī)數(shù)的實現(xiàn)方法總結(jié)
這篇文章主要介紹了Java中生成隨機(jī)數(shù)的實現(xiàn)方法總結(jié),其中多線程并發(fā)的實現(xiàn)方式尤為exciting,需要的朋友可以參考下2015-11-11Java fastjson解析json字符串實現(xiàn)過程解析
這篇文章主要介紹了Java fastjson解析json字符串實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10Springboot中MyBatisplus使用IPage和Page分頁的實例代碼
這篇文章主要介紹了Springboot中MyBatisplus使用IPage和Page分頁,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12JAVA 筆記 ClassLoader.getResourceAsStream() 與 Class.getResourc
這篇文章主要介紹了JAVA 筆記 ClassLoader.getResourceAsStream() 與 Class.getResourceAsStream()的區(qū)別,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07如何用Dos命令運(yùn)行Java版HelloWorld你知道嗎
這篇文章主要介紹了在dos窗口中編譯和運(yùn)行java文件的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08Java之next()、nextLine()區(qū)別及問題解決
這篇文章主要介紹了Java之next()、nextLine()區(qū)別及問題解決,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08