1、类方法,也可以说是静态方法,一半是使用static修饰,这类方法,不用实例化类就可调用
2、实例方法,我们一把常用的方法都是实例方法,不带static修饰,使用的时候需要先实例化类
3、构造方法,写法为public XXX(){},这个是在实例化类的时候,调用的方法,如果不显示写有默认的构造方法
4、静态初始化方法,就是类里面static 包含的方法,没有方法名,如static{},这个只会执行一次,并且如果创建子类对象时,同时也会导致父类静态初始化方式执行
举例
public class Root { static{ System.out.println("Root的静态初始化块"); } { System.out.println("Root的普通初始化块"); } public Root(){ System.out.println("Root的无参数的构造器"); } }
public class Child extends Root{ static{ System.out.println("Child的静态初始化块"); } { System.out.println("Child的普通初始化块"); } public Child(){ System.out.println("Child的无参数的构造器"); } public Child(String msg){ this(); System.out.println("Child的带参数构造器,其参数值:" + msg); } }
public class Grandson extends Child { static{ System.out.println("Grandson的静态初始化块"); } { System.out.println("Grandson的普通初始化块"); } public Grandson() { //通过super调用父类中有一个字符串参数的构造器 super("Java初始化顺序演示"); System.out.println("执行Grandson的构造器"); } }
执行
public class MethodShow { /** * * @param args * @see main */ public static void main(String[] args) { new Grandson(); } }
结果
Root的静态初始化块
Child的静态初始化块
Grandson的静态初始化块
Root的普通初始化块
Root的无参数的构造器
Child的普通初始化块
Child的无参数的构造器
Child的带参数构造器,其参数值:Java初始化顺序演示
Grandson的普通初始化块
执行Grandson的构造器