下面通过例子来说明:
父类
public class father {
static{//静态块
system.out.println("father'sstatic free block running");
}
{//非静态块
system.out.println("father'sfree block running");
}
public father(){
system.out.println("father'sconstructor running");
}
}
子类
{//非静态块
system.out.println("son's freeblock running");
}
public son() {
// todo auto-generated constructor stub
system.out.println("son'sconstructor running");
}
}
主函数所在类
public class test{
public static void main(string[] args) {
class f;
try {
system.out.println("--------beforeload father--------");
f=class.forname("freeblock.father");
system.out.println("--------afterload father---------");
system.out.println("--------beforeinitial father object--------");
f.newinstance();
system.out.println("--------afterinitial father object--------");
} catch (classnotfoundexception e) {
e.printstacktrace();
} catch (instantiationexception e) {
e.printstacktrace();
} catch (illegalaccessexception e) {
e.printstacktrace();
}
class s;
try {
system.out.println("-------beforeload son--------");
s=class.forname("freeblock.son");
system.out.println("--------afterload son-------");
system.out.println("--------beforeinitial son object----------");
s.newinstance();
system.out.println("--------afterinitial son object-----------");
} catch (classnotfoundexception e) {
e.printstacktrace();
} catch (instantiationexception e) {
e.printstacktrace();
} catch (illegalaccessexception e) {
e.printstacktrace();
}
}
}
执行结果:
--------before loadfather--------
father's static free blockrunning
--------after loadfather---------
--------before initial fatherobject--------
father's free block running
father's constructor running
--------after initial fatherobject--------
-------before load son--------
son's static free block running
--------after load son-------
--------before initial sonobject----------
father's free block running
father's constructor running
son's free block running
son's constructor running
--------after initial son object-----------
如对本文有疑问, 点击进行留言回复!!
第三次学JAVA再学不好就吃翔(part88)--ArrayList嵌套ArrayList
使用ffmpeg视频切片并加密和视频AES-128加密后播放
JAVA程序设计:最长重复子串(LeetCode:1044)
LiveGBS国标GB/T28181云端录像分布式录像存储自动清理时移回看录像下载播放
网友评论