代码块
面试题?
静态<a href="https://www.jb51.cc/tag/daima/" target="_blank" class="keywords">代码</a>块,构造<a href="https://www.jb51.cc/tag/daima/" target="_blank" class="keywords">代码</a>块,构造<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>的执行顺序?
静态<a href="https://www.jb51.cc/tag/daima/" target="_blank" class="keywords">代码</a>块 -- 构造<a href="https://www.jb51.cc/tag/daima/" target="_blank" class="keywords">代码</a>块 -- 构造<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>
静态<a href="https://www.jb51.cc/tag/daima/" target="_blank" class="keywords">代码</a>块:只执行一次
构造<a href="https://www.jb51.cc/tag/daima/" target="_blank" class="keywords">代码</a>块:每次<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>构造<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>都执行
<span style="color: #008000;">*/<span style="color: #0000ff;">class<span style="color: #000000;"> Code {
<span style="color: #0000ff;">static<span style="color: #000000;"> {
<span style="color: #0000ff;">int a = 1000<span style="color: #000000;">;
System.out.println(a);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">构造<a href="https://www.jb51.cc/tag/daima/" target="_blank" class="keywords">代码</a>块</span>
<span style="color: #000000;"> {
<span style="color: #0000ff;">int x = 100<span style="color: #000000;">;
System.out.println(x);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">构造<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a></span>
<span style="color: #0000ff;">public</span><span style="color: #000000;"> Code(){
System.out.println(</span>"code"<span style="color: #000000;">);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">构造<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a></span>
<span style="color: #0000ff;">public</span> Code(<span style="color: #0000ff;">int</span><span style="color: #000000;"> a){
System.out.println(</span>"code"<span style="color: #000000;">);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">构造<a href="https://www.jb51.cc/tag/daima/" target="_blank" class="keywords">代码</a>块</span>
<span style="color: #000000;"> {
<span style="color: #0000ff;">int y = 200<span style="color: #000000;">;
System.out.println(y);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">静态<a href="https://www.jb51.cc/tag/daima/" target="_blank" class="keywords">代码</a>块</span>
<span style="color: #0000ff;">static</span><span style="color: #000000;"> {
</span><span style="color: #0000ff;">int</span> b = 2000<span style="color: #000000;">;
System.out.println(b);
}
}
<span style="color: #0000ff;">class<span style="color: #000000;"> CodeDemo {
<span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #008000;">//<span style="color: #008000;">局部代码块
<span style="color: #000000;"> {
<span style="color: #0000ff;">int x = 10<span style="color: #000000;">;
System.out.println(x);
}
<span style="color: #008000;">//<span style="color: #008000;">找不到符号
<span style="color: #008000;">//<span style="color: #008000;">System.out.println(x);
<span style="color: #000000;"> {
<span style="color: #0000ff;">int y = 20<span style="color: #000000;">;
System.out.println(y);
}
System.out.println("---------------"<span style="color: #000000;">);
Code c </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Code();
System.out.println(</span>"---------------"<span style="color: #000000;">);
Code c2 </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Code();
System.out.println(</span>"---------------"<span style="color: #000000;">);
Code c3 </span>= <span style="color: #0000ff;">new</span> Code(1<span style="color: #000000;">);
}
}
看程序,写结果:
执行顺序:
林青霞都60了,我很伤心
我是main<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a> ---
Student 静态<a href="https://www.jb51.cc/tag/daima/" target="_blank" class="keywords">代码</a>块
Student 构造<a href="https://www.jb51.cc/tag/daima/" target="_blank" class="keywords">代码</a>块
Student 构造<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>
Student 构造<a href="https://www.jb51.cc/tag/daima/" target="_blank" class="keywords">代码</a>块
Student 构造<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>
<span style="color: #008000;">*/<span style="color: #0000ff;">class<span style="color: #000000;"> Student {
<span style="color: #008000;">//<span style="color: #008000;">C
<span style="color: #0000ff;">static<span style="color: #000000;"> {
System.out.println("Student 静态代码块"<span style="color: #000000;">);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">D</span>
<span style="color: #0000ff;">public</span><span style="color: #000000;"> Student() {
System.out.println(</span>"Student 构造<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>"<span style="color: #000000;">);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">E</span>
<span style="color: #000000;"> {
System.out.println("Student 构造代码块"<span style="color: #000000;">);
}
}
<span style="color: #0000ff;">class<span style="color: #000000;"> StudentDemo {
<span style="color: #0000ff;">static<span style="color: #000000;"> {
<span style="color: #008000;">//<span style="color: #008000;">A
System.out.println("林青霞都60了,我很伤心"<span style="color: #000000;">);
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {
</span><span style="color: #008000;">//</span><span style="color: #008000;">B</span>
System.out.println("我是main<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>"<span style="color: #000000;">);
Student s1 </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Student();
Student s2 </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Student();
}
}
原文链接:https://www.f2er.com/java/403349.html