本文主要以白话的形式 ‘简单’ 的描述在 java 中 new 对象的过程,之所以说是 ‘简单’ 的描述是因为,在本文中不会讲述底层的加载过程。
首先认识几个知识点:
知道以上三个知识点后,那么就先创建一个Person类,观察一下
Person.java
package cn.ttext.test.init;
public class Person {
private String name;
private int age;
private String sex;
static {
System.out.println("静态代码块1");
}
{
System.out.println("构造代码块1");
}
static {
System.out.println("静态代码块2");
}
{
System.out.println("构造代码块2");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
对其编译,使用IDEA的反编译功能查看class文件内容
Person.class
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package cn.ttext.test.init;
public class Person {
private String name;
private int age;
private String sex;
public Person() {
System.out.println("构造代码块1");
System.out.println("构造代码块2");
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
static {
System.out.println("静态代码块1");
System.out.println("静态代码块2");
}
}
首先观察到,编译后的代码,和咱写的代码有点区别:
下面再在Person类中,显示声明多个构造函数。
Person.java
package cn.ttext.test.init;
public class Person {
private String name;
private int age;
private String sex;
static {
System.out.println("静态代码块1");
}
{
System.out.println("构造代码块1");
}
public Person(String name) {
this.name = name;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
static {
System.out.println("静态代码块2");
}
{
System.out.println("构造代码块2");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
查看编译后的内容
Person.class
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package cn.ttext.test.init;
public class Person {
private String name;
private int age;
private String sex;
public Person(String name) {
System.out.println("构造代码块1");
System.out.println("构造代码块2");
this.name = name;
}
public Person(String name, int age) {
System.out.println("构造代码块1");
System.out.println("构造代码块2");
this.name = name;
this.age = age;
}
public Person(String name, int age, String sex) {
System.out.println("构造代码块1");
System.out.println("构造代码块2");
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
static {
System.out.println("静态代码块1");
System.out.println("静态代码块2");
}
}
看到编译后的结果说明几个问题:
那现在看一下创建Person对象的控制台输出
Main.java
package cn.ttext.test.init;
public class Main {
public static void main(String[] args) {
new Person("张三",18,"女");
System.out.println();
new Person("李四",18,"男");
}
}
因为静态代码块只会随着类的加载而运行,所以第二次创建对象时,静态代码块没有运行。 结合上边的知识点看,是不是更明白了呢。
转发请注明本文链接。