Java淺析代碼塊與構(gòu)造塊及靜態(tài)塊三者之間的關(guān)系
普通代碼塊(本地代碼塊)
定義在方法里面用{ }括起來的代碼塊(凡是用{ }括起來都叫代碼塊,只是沒有細分),就是普通代碼塊,也叫本地代碼塊。(不多見,也不常用)
例如:
public class Test {
public static void main(String[] args){
{
System.out.println("本地代碼塊!");
}
}
}構(gòu)造塊(實例代碼塊)
實例代碼塊定義在類的內(nèi)部,方法的外部,可以初始化實例的成員
例如:
class Student{
//成員變量
public String name;
public int score;
//類變量
public static String classes = "火箭6班";
//類方法
public static void fun1(){
Student stu = new Student();
stu.name = "jay"; //正確
System.out.println("類方法");
}
//成員方法
public void fun2(String name, int score){
this.name = name;
this.score = score;
}
//實例代碼塊
{
this.name = "jay";
System.out.println("實例代碼塊!");
}
}靜態(tài)塊(靜態(tài)代碼塊)
靜態(tài)代碼塊定義在類的內(nèi)部,方法的外部,可以初始化實例的成員
例如:
class Student{
//成員變量
public String name;
public int score;
//類變量
public static String classes = "火箭6班";
//類方法
public static void fun1(){
Student stu = new Student();
stu.name = "jay"; //正確
System.out.println("類方法");
}
//成員方法
public void fun2(String name, int score){
this.name = name;
this.score = score;
}
//實例代碼塊
{
System.out.println("實例代碼塊!");
}
//靜態(tài)代碼塊
static{
classes = "16班";
System.out.println("靜態(tài)代碼塊!");
}他們之間有什么關(guān)系呢
例子
情況一:看如下代碼會執(zhí)行什么?
class Student{
//成員變量
public String name;
public int score;
//構(gòu)造方法
public Student(){
System.out.println("構(gòu)造方法!");
}
//類變量
public static String classes = "火箭6班";
//類方法
public static void fun1(){
Student stu = new Student();
stu.name = "jay"; //正確
System.out.println("類方法");
}
//成員方法
public void fun2(String name, int score){
this.name = name;
this.score = score;
}
//實例代碼塊
{
System.out.println("實例代碼塊!");
}
//靜態(tài)代碼塊
static{
System.out.println("靜態(tài)代碼塊!");
}
}
public class Test {
public static void main(String[] args){
Student stu = new Student();
}
}情況一運行結(jié)果:

情況二:看如下代碼會執(zhí)行什么?
class Student{
//成員變量
public String name;
public int score;
//構(gòu)造方法
public Student(){
System.out.println("構(gòu)造方法!");
}
//類變量
public static String classes = "火箭6班";
//類方法
public static void fun1(){
Student stu = new Student();
stu.name = "jay"; //正確
System.out.println("類方法");
}
//成員方法
public void fun2(String name, int score){
this.name = name;
this.score = score;
}
//實例代碼塊
{
System.out.println("實例代碼塊!");
}
//靜態(tài)代碼塊
static{
System.out.println("靜態(tài)代碼塊!");
}
}
public class Test {
public static void main(String[] args){
System.out.println(Student.classes);
}
}情況二運行結(jié)果:

分析
- 他們執(zhí)行的順序是:靜態(tài)代碼塊 -> 實例代碼塊 -> 構(gòu)造方法(不論代碼塊的位置在哪,都是這個順序!若有多個靜態(tài),看定義順序即可)
- 只要加載了類,靜態(tài)代碼塊就會被執(zhí)行
- 若沒有實例化對象只會執(zhí)行靜態(tài)的
- 若有多個實例,須看定義順序
- 如果沒有實例化對象,靜態(tài)代碼塊只會執(zhí)行一次
到此這篇關(guān)于Java淺析代碼塊與構(gòu)造塊及靜態(tài)塊三者之間的關(guān)系的文章就介紹到這了,更多相關(guān)Java 代碼塊 構(gòu)造塊 靜態(tài)塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring mvc4的日期/數(shù)字格式化、枚舉轉(zhuǎn)換示例
本篇文章主要介紹了spring mvc4的日期/數(shù)字格式化、枚舉轉(zhuǎn)換示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01
java使用mybatis調(diào)用存儲過程返回一個游標結(jié)果集方式
這篇文章主要介紹了java使用mybatis調(diào)用存儲過程返回一個游標結(jié)果集方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
SpringBoot中全局異常處理的5種實現(xiàn)方式小結(jié)
在實際開發(fā)中,異常處理是一個非常重要的環(huán)節(jié),合理的異常處理機制不僅能提高系統(tǒng)的健壯性,還能大大提升用戶體驗,下面我們就來看看SpringBoot中全局異常處理的5種實現(xiàn)方式吧2025-03-03

