完全填平Static坑——細節決定成敗

學習的過程就是填坑的過程,可不要偷懶想着跳過去,你如今跳過去,就至關於給本身挖了一個坑,你早晚會掉進去的,爲了不不掉坑,因此,努力填坑吧!ide

若是沒有static會怎樣?
需求:函數

一、定義Student類學習

1:姓名、國籍,說話行爲

  2:多個構造,重載形式體現

二、學生的國籍都是肯定的this

1:國籍能夠進行顯示初始化
public

class

Student
 {

String
 name;
//姓名

String
 country;
//國籍

public

Student
(
String
 name, 
String
 country) {

this
.name = name;

this
.country = country;

    }

public

void
 speak(){

System
.out.println(
"姓名:"
+
this
.name+
" "
+
"國籍:"
+
this
.country);

    }

}

class

Test
{

public

static

void
 main(
String
[] args) {

Student
 student = 
new

Student
(
"何道昌"
,
"中國"
);

Student
 student1 = 
new

Student
(
"孫雙雙"
,
"中國"
);

        student.speak();

        student1.speak();

    }

}

運行結果: code

姓名:何晶晶 國籍:中國

姓名:孫雙雙 國籍:中國

目前存在的問題:對象

如今咱們已知學生都是中國人,如今咱們每建立一個學生對象,就要給全部學生的國籍屬性賦相同的值,這樣形成堆內存空間資源浪費

目前方案:blog

把「中國」這個數據移動到數據共享區中,共享這個數據給全部的Student對象使用便可

    疑問:如何才能把這個數據移動到數據共享區中共享呢?

解決方案:生命週期

只須要用static修飾該數據便可

    靜態的成員變量只會在數據共享區中維護一份,而非靜態成員變量的數據會在每一個對象中都維護一份

完全填平Static坑——細節決定成敗

public

class

Student
 {

String
 name;
//姓名

//使用了static修飾country,那麼這時候country就是一個共享的數據

static

String
 country = 
"中國"
;
//國籍

//構造函數

public

Student
(
String
 name) {

this
.name = name;

    }

//說話行爲

public

void
 speak(){

System
.out.println(
"姓名:"
+
this
.name+
" "
+
"國籍:"
+country);

    }

}

class

Test
{

public

static

void
 main(
String
[] args) {

Student
 student = 
new

Student
(
"何道昌"
);

Student
 student1 = 
new

Student
(
"孫雙雙"
);

        student.speak();

        student1.speak();

    }

}

運行結果: 內存

姓名:何晶晶 國籍:中國

姓名:孫雙雙 國籍:中國

下面我來詳細解說static資源

static(靜態修飾符)
1.static修飾靜態變量

若是有數據須要被共享給全部對象使用時,那麼就可使用static修飾

靜態成員變量的訪問方式:

方式一:可使用對象進行訪問

格式:對象.變量名

方式二:可使用類名進行訪問

格式:類名.變量名

注意:

1). 非靜態的成員變量只能使用對象進行訪問,不能使用類命進行訪問

public

class

Student
 {

String
 name;
//姓名     非靜態成員變量

//使用了static修飾country,那麼這時候country就是一個共享的數據

static

String
 country = 
"中國"
;
//國籍    靜態成員變量

//構造函數

public

Student
(
String
 name) {

this
.name = name;

    }

//說話行爲

public

void
 speak(){

System
.out.println(
"姓名:"
+
this
.name+
" "
+
"國籍:"
+country);

    }

}

class

Test
{

public

static

void
 main(
String
[] args) {

Student
 student = 
new

Student
(
"何道昌"
);

System
.out.println(student.name);
//用對象訪問非靜態變量<br>        Systen.out.println(student.country);//用對象訪問靜態變量

System
.out.println(
Student
.country);
//用類命訪問靜態變量

}

運行結果:

何道晶<br>中國

中國

2). 千萬不要爲了方便訪問數據而使用static修飾成員變量,只有成員變量的數據真正須要被共享的時候,才使用static修飾

static修飾成員變量的應用場景:若是一個數據須要被全部對象共享使用的時候,用static修飾

2.static修飾成員函數(靜態的成員方法)

靜態成員函數的訪問方式:

方式一:可使用對象進行訪問

格式:對象.靜態的函數名

方式二:可使用類名進行訪問

格式:類名.靜態的函數名

推薦使用類名直接訪問靜態的成員

緣由:

1.方便      

    2.節省內存

靜態函數要注意的事項:

1.靜態函數是能夠調用類名或者對象進行調用的,而非靜態函數只能使用對象進行調用

    2.靜態的函數能夠訪問靜態的成員,可是不能直接訪問非靜態的成員

    3.非靜態的函數是能夠直接訪問靜態與非靜態的成員

    4.靜態函數不能出現this或者super關鍵字
public

class

Student
 {

String
 name;
//姓名     非靜態成員變量

//使用了static修飾country,那麼這時候country就是一個共享的數據

static

String
 country = 
"中國"
;
//國籍    靜態成員變量

//構造函數

public

Student
(
String
 name) {

this
.name = name;

    }

//說話行爲

//靜態成員方法

public

static

void
 speak(){

System
.out.println(
"國籍:"
+country);

    }

//學習行爲

//非靜態成員方法

public

void
 study(){

System
.out.println(name+
"好好學習"
);

    }

}

class

Test
{

public

static

void
 main(
String
[] args) {

Student
 student = 
new

Student
(
"何道昌"
);

System
.out.println(student.name);
//用對象訪問非靜態變量

System
.out.println(student.country);
//用對象訪問靜態變量

System
.out.println(
Student
.country);
//用類命訪問靜態變量

        student.study();
//用對象訪問非靜態方法

        student.speak();
//用對象訪問靜態方法

Student
.speak();
//用類名訪問靜態方法

    }

}

運行結果:

何道晶

中國

中國

何道晶好好學習

國籍:中國

國籍:中國

靜態的成員變量與非靜態的成員變量的區別:

1)、做用上的區別:

1.靜態的成員變量的做用是共享一個數據給全部的對象使用

2.非靜態的成員變量的做用是描述一類事物的公共屬性

2)、數量和存儲位置上的區別:

1.靜態成員變量是在存儲方法區內存中,並且只會存在一份數據

2.非靜態的成員變量是存儲在堆內存中,有n個對象就有n份數據

3)、生命週期的區別:

1.靜態的成員變量數據是隨着類的加載而存在,隨着類文件的消失而消失

2.非靜態的成員變量數據是隨着對象的建立而存在,隨着對象被垃圾回收器回收而消失

靜態函數不能訪問非靜態的成員?

靜態函數只要存在有對象,那麼也能夠訪問非靜態的數據,只是不能直接訪問。

最後,繼續用這個例子穿插一下靜態代碼塊的知識

靜態代碼塊是在Student.class文件加載到內存的時候就立刻執行的

public

class

Student
 {

String
 name;
//姓名     非靜態成員變量

//使用了static修飾country,那麼這時候country就是一個共享的數據

static

String
 country = 
"中國"
;
//國籍    靜態成員變量

//靜態代碼塊

static
{

System
.out.println(
"靜態代碼塊執行了!!"
);

    }

//構造函數

public

Student
(
String
 name) {

this
.name = name;

    }

//說話行爲

//靜態成員方法

public

static

void
 speak(){

System
.out.println(
"國籍:"
+country);

    }

//學習行爲

//非靜態成員方法

public

void
 study(){

System
.out.println(name+
"好好學習"
);

    }

}

class

Test
{

public

static

void
 main(
String
[] args) {

Student
.speak();

    }

}

運行結果:

靜態代碼塊執行了!!

國籍:中國

理解到這,你再看看下面的分析圖,應該還會有所收穫

完全填平Static坑——細節決定成敗

最後再跟你們分享一句話:

一我的若想改變本身的命運,最重要的是要改變本身,改變心態,改變環境,這樣命運也會隨之改變

讀完此篇,如若對你有用,記得推薦,如如有誤,歡迎指正!

相關文章
相關標籤/搜索