一般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在不创建对象的情况下,其他程序来调用的时候,需要使用静态方法,这种代码是被动执行的. 静态方法在类加载的时候 就已经加载 可以用类名直接调用 比如main方法就必须是静态的 这是程序入口。
两者的区别就是:静态代码块是自动执行的; 静态方法是被调用的时候才执行的.
静态方法
(1)在Java里,可以定义一个不需要创建对象的方法,这种方法就是静态方法。要实现这样的效果,只需要在类中定义的方法前加上static关键字。例如:public static int maximum(int n1,int n2)
使用类的静态方法时,注意:
a.在静态方法里只能直接调用同类中其他的静态成员(包括变量和方法),而不能直接访问类中的非静态成员。这是因为,对于非静态的方法和变量,需要先创建类的实例对象后才可使用,而静态方法在使用前不用创建任何对象。
b.静态方法不能以任何方式引用this和super关键字,因为静态方法在使用前不用创建任何实例对象,当静态方法调用时,this所引用的对象根本没有产生(this关键字只能在方法内部使用,表示对“调用方法的那个对象”的引用)。
(2)静态变量是属于整个类的变量而不是属于某个对象的。注意不能把任何方法体内的变量声明为静态,例如:
fun()
{
static int i=0;//非法。
}
(3)一个类可以使用不包含在任何方法体中的静态代码块,当类被载入时,静态代码块被执行,且之被执行一次,静态块常用来执行类属性的初始化。例如:
static
{
}
(4)非静态代码块
public class TestStaticCon { 1. public static int a = 0; 2. 3. static { 4. a = 10; 5. System.out.println("父类的非静态代码块在执行a=" + a); 6. } 7. 8. { 9. a = 8; 10. System.out.println("父类的非静态代码块在执行a=" + a); 11. } 12. 13. public TestStaticCon() { 14. this("a在父类带参构造方法中的值:" + TestStaticCon.a); // 调用另外一个构造方法 15. System.out.println(a); 16. System.out.println("父类无参构造方法在执行a=" + a); 17. } 18. 19. public TestStaticCon(String n) { 20. System.out.println(n); 21. System.out.println(a); 22. 23. } 24. 25. public static void main(String[] args) { 26. TestStaticCon tsc = null; 27. System.out.println("!!!!!!!!!!!!!!!!!!!!!"); 28. tsc = new TestStaticCon(); 29. }/*Output: 30. 父类的非静态代码块在执行a=10 31. !!!!!!!!!!!!!!!!!!!!! 32. 父类的非静态代码块在执行a=8 33. a在父类带参构造方法中的值:10 34. 8 35. 8 36. 父类无参构造方法在执行a=8 37. */ 38. 39. } public class TestStaticCon { 1. public static int a = 0; 2. 3. static { 4. a = 10; 5. System.out.println("父类的非静态代码块在执行a=" + a); 6. } 7. 8. { 9. a = 8; 10. System.out.println("父类的非静态代码块在执行a=" + a); 11. } 12. 13. public TestStaticCon() { 14. this("a在父类带参构造方法中的值:" + TestStaticCon.a); // 调用另外一个构造方法 15. System.out.println(a); 16. System.out.println("父类无参构造方法在执行a=" + a); 17. } 18. 19. public TestStaticCon(String n) { 20. System.out.println(n); 21. System.out.println(a); 22. 23. } 24. 25. public static void main(String[] args) { 26. TestStaticCon tsc = null; 27. System.out.println("!!!!!!!!!!!!!!!!!!!!!"); 28. tsc = new TestStaticCon(); 29. }/*Output: 30. 父类的非静态代码块在执行a=10 31. !!!!!!!!!!!!!!!!!!!!! 32. 父类的非静态代码块在执行a=8 33. a在父类带参构造方法中的值:10 34. 8 35. 8 36. 父类无参构造方法在执行a=8 37. */ 38. 39. }
结论:静态代码块是在类加载时自动执行的,非静态代码块是在创建对象时自动执行的代码,不创建对象不执行该类的非静态代码块。
附录:面试题
1. public class Father { 2. static { 3. System.out.println("Father static Create"); 4. } 5. { 6. System.out.println("Father Create"); 7. } 8. 9. public static void StaticFunction(){ 10. System.out.println("Father static Function"); 11. } 12. 13. public void Function(){ 14. System.out.println("Father Function"); 15. } 16. } 1. public class ChildOne extends Father { 2. static { 3. System.out.println("ChildOne static Create"); 4. } 5. { 6. System.out.println("ChildOne Create"); 7. } 8. 9. public static void StaticFunction(){ 10. System.out.println("ChildOne static Function"); 11. } 12. 13. } 1. public class ChildTwo extends Father { 2. static { 3. System.out.println("ChildTwo static Create"); 4. } 5. { 6. System.out.println("ChildTwo Create"); 7. } 8. 9. public static void StaticFunction() { 10. System.out.println("ChildTwo static Function"); 11. } 12. 13. public void Function() { 14. System.out.println("ChildTwo Function"); 15. } 16. } 1. public class Main { 2. public static void main(String[] args) { 3. Father A = new ChildOne(); 4. Father B = new ChildTwo(); 5. A.StaticFunction(); 6. A.Function(); 7. B.StaticFunction(); 8. B.Function(); 9. }