作用域与生命周期
理解变量在哪些位置可见(作用域)以及它们存在多久(生命周期):代码块、循环、方法、字段、变量遮蔽和 final 参数。
作用域与生命周期 是 CoddyKit 上的免费 Java Academy 课时。 这是第 3 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Java Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Java Academy 课程共包含 3 节课。
作用域与生命周期
作用域 = 名称(变量)可见的范围。生命周期 = 变量存在的时长。您将学习代码块作用域、循环作用域、方法局部变量、类字段以及遮蔽规则。
代码块作用域
代码块作用域:在大括号内声明的变量只能在这些大括号内可见。
public class Main {
public static void main(String[] args) {
int a = 10; // visible in main
{
int b = 20; // visible only inside this block
System.out.println("inside block: a=" + a + ", b=" + b);
}
System.out.println("outside block: a=" + a);
// System.out.println(b); // would NOT compile: b is out of scope
}
}
循环作用域
循环作用域:在 for 头部声明的循环索引只能在循环体内可见。
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 3; i = i + 1) {
System.out.println("i in loop = " + i);
}
// System.out.println(i); // would NOT compile: i is scoped to the loop
int j;
for (j = 0; j < 2; j = j + 1) {
System.out.println("j in loop = " + j);
}
System.out.println("j after loop = " + j); // OK: j declared outside
}
}
字段与局部变量
生命周期:类字段或静态字段会跨方法调用存在;局部变量会在每次调用时重新创建,并在方法结束时被丢弃。
public class Main {
// static field (class-level): lives for the program lifetime
static int counter = 0;
static void tick() {
int local = 100; // local: created each call, dies when method returns
counter = counter + 1;
System.out.println("tick: counter=" + counter + ", local=" + local);
}
public static void main(String[] args) {
tick();
tick();
tick();
System.out.println("final counter=" + counter);
}
}
名称遮蔽
名称遮蔽:内部声明(例如参数或局部变量)可以隐藏具有相同标识符的外部名称。在实例方法中,使用 ClassName.field(或 this.field)访问被隐藏的字段。
public class Main {
static String label = "FIELD"; // class field
static void demo(String label) { // parameter shadows field name
System.out.println("param label = " + label); // uses parameter
{
String labelInner = "INNER"; // new name in inner block
System.out.println("inner block labelInner = " + labelInner);
System.out.println("still param label = " + label);
}
System.out.println("field via class name = " + Main.label); // refer to field explicitly
}
public static void main(String[] args) {
demo("PARAM");
}
}
不可重新赋值与按值传递
不可重新赋值的参数不能再次赋值。Java 采用按值传递:重新给参数赋值不会改变调用方的变量。
public class Main {
// final parameter cannot be reassigned inside the method
static int inc(final int n) {
// n = n + 1; // would NOT compile if uncommented
return n + 1; // safe: return a new value
}
static void reassign(int x) {
x = 999; // reassignes the local copy; caller variable is unchanged
}
public static void main(String[] args) {
int a = 5;
int b = inc(a);
System.out.println("a=" + a + ", b=inc(a)=" + b);
reassign(a);
System.out.println("after reassign(a): a is still " + a);
}
}
作用域与生命周期检查
快速检查:关于作用域和生命周期的哪项说法是 TRUE?
回顾与下一步
回顾:您练习了代码块和循环作用域、字段与局部变量、名称遮蔽以及不可重新赋值的参数。下一步:在 A2.2 中继续学习设计与复用模式。
常见问题解答
「作用域与生命周期」课时是免费的吗?
是的 — 「作用域与生命周期」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Java Academy 课程的其余内容,请升级到 CoddyKit PRO。 Java Academy 课程共包含 3 节课。
「作用域与生命周期」这节课中我会学到什么?
理解变量在哪些位置可见(作用域)以及它们存在多久(生命周期):代码块、循环、方法、字段、变量遮蔽和 final 参数。 你通过在浏览器中直接运行的动手代码来练习 Java Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Java Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Java Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 3 节。
「作用域与生命周期」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Java Academy 课中编写并运行代码吗?
能。每节 Java Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。