package newprac; public class Static { String match;//声明String型变量name String time;//声明char型变量sex static String score;//声明整型变量age //通过构造方法赋值变量name public Static(){ match = "football match"; System.out.println("比赛"+match); } { time = "02:00"; System.out.println("时间"+time); } static{ score = "2:1"; System.out.println("比分"+score); } public static void main(String[] args){ } }
输出结果:
比分2:1
public class Static { String match;//声明String型变量name String time;//声明char型变量sex static String score;//声明整型变量age //通过构造方法赋值变量name public void Static(){ match = "football match"; System.out.println("比赛"+match); } { time = "02:00"; System.out.println("时间"+time); } static{ score = "2:1"; System.out.println("比分"+score); } public static void main(String[] args){ Static st = new Static(); st.Static(); } }
在main函数中 构建一个方法 Static bf=new Static();
然后调用它就可以了 bf.Static();
main函数里面没有建立对象