java 静态相关内容

一般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在不创建对象的情况下,其他程序来调用的时候,需要使用静态方法,这种代码是被动执行的. 静态方法在类加载的时候 就已经加载 可以用类名直接调用 比如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)非静态代码块 

一般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在不创建对象的情况下,其他程序来调用的时候,需要使用静态方法,这种代码是被动执行的. 静态方法在类加载的时候 就已经加载 可以用类名直接调用 比如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)非静态代码块 
[java] view plain copy
 
  1. public class TestStaticCon {     
  2.   
  3.    1.     public static int a = 0;     
  4.    2.     
  5.    3.     static {     
  6.    4.         a = 10;     
  7.    5.         System.out.println("父类的非静态代码块在执行a=" + a);     
  8.    6.     }     
  9.    7.          
  10.    8.     {     
  11.    9.         a = 8;     
  12.   10.         System.out.println("父类的非静态代码块在执行a=" + a);     
  13.   11.     }     
  14.   12.     
  15.   13.     public TestStaticCon() {     
  16.   14.         this("a在父类带参构造方法中的值:" + TestStaticCon.a); // 调用另外一个构造方法     
  17.   15.         System.out.println(a);     
  18.   16.         System.out.println("父类无参构造方法在执行a=" + a);     
  19.   17.     }     
  20.   18.     
  21.   19.     public TestStaticCon(String n) {     
  22.   20.         System.out.println(n);     
  23.   21.         System.out.println(a);     
  24.   22.     
  25.   23.     }     
  26.   24.     
  27.   25.     public static void main(String[] args) {     
  28.   26.         TestStaticCon tsc = null;     
  29.   27.         System.out.println("!!!!!!!!!!!!!!!!!!!!!");     
  30.   28.         tsc = new TestStaticCon();     
  31.   29.     }/*Output:   
  32.   30.            父类的非静态代码块在执行a=10   
  33.   31.            !!!!!!!!!!!!!!!!!!!!!   
  34.   32.            父类的非静态代码块在执行a=8   
  35.   33.            a在父类带参构造方法中的值:10   
  36.   34.            8   
  37.   35.            8   
  38.   36.            父类无参构造方法在执行a=8   
  39.   37.            */    
  40.   38.     
  41.   39. }    
  42.   
  43. public class TestStaticCon {     
  44.   
  45.    1.     public static int a = 0;     
  46.    2.     
  47.    3.     static {     
  48.    4.         a = 10;     
  49.    5.         System.out.println("父类的非静态代码块在执行a=" + a);     
  50.    6.     }     
  51.    7.          
  52.    8.     {     
  53.    9.         a = 8;     
  54.   10.         System.out.println("父类的非静态代码块在执行a=" + a);     
  55.   11.     }     
  56.   12.     
  57.   13.     public TestStaticCon() {     
  58.   14.         this("a在父类带参构造方法中的值:" + TestStaticCon.a); // 调用另外一个构造方法     
  59.   15.         System.out.println(a);     
  60.   16.         System.out.println("父类无参构造方法在执行a=" + a);     
  61.   17.     }     
  62.   18.     
  63.   19.     public TestStaticCon(String n) {     
  64.   20.         System.out.println(n);     
  65.   21.         System.out.println(a);     
  66.   22.     
  67.   23.     }     
  68.   24.     
  69.   25.     public static void main(String[] args) {     
  70.   26.         TestStaticCon tsc = null;     
  71.   27.         System.out.println("!!!!!!!!!!!!!!!!!!!!!");     
  72.   28.         tsc = new TestStaticCon();     
  73.   29.     }/*Output:   
  74.   30.            父类的非静态代码块在执行a=10   
  75.   31.            !!!!!!!!!!!!!!!!!!!!!   
  76.   32.            父类的非静态代码块在执行a=8   
  77.   33.            a在父类带参构造方法中的值:10   
  78.   34.            8   
  79.   35.            8   
  80.   36.            父类无参构造方法在执行a=8   
  81.   37.            */    
  82.   38.     
  83.   39. }    


[java] view plain copy
 
  1. public class TestStaticCon {  
  2.     public static int a = 0;  
  3.   
  4.     static {  
  5.         a = 10;  
  6.         System.out.println("父类的非静态代码块在执行a=" + a);  
  7.     }  
  8.       
  9.     {  
  10.         a = 8;  
  11.         System.out.println("父类的非静态代码块在执行a=" + a);  
  12.     }  
  13.   
  14.     public TestStaticCon() {  
  15.         this("a在父类带参构造方法中的值:" + TestStaticCon.a); // 调用另外一个构造方法  
  16.         System.out.println(a);  
  17.         System.out.println("父类无参构造方法在执行a=" + a);  
  18.     }  
  19.   
  20.     public TestStaticCon(String n) {  
  21.         System.out.println(n);  
  22.         System.out.println(a);  
  23.   
  24.     }  
  25.   
  26.     public static void main(String[] args) {  
  27.         TestStaticCon tsc = null;  
  28.         System.out.println("!!!!!!!!!!!!!!!!!!!!!");  
  29.         tsc = new TestStaticCon();  
  30.     }/*Output: 
  31.            父类的非静态代码块在执行a=10 
  32.            !!!!!!!!!!!!!!!!!!!!! 
  33.            父类的非静态代码块在执行a=8 
  34.            a在父类带参构造方法中的值:10 
  35.            8 
  36.            8 
  37.            父类无参构造方法在执行a=8 
  38.            */  
  39.   
  40. }  

一般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在不创建对象的情况下,其他程序来调用的时候,需要使用静态方法,这种代码是被动执行的. 静态方法在类加载的时候 就已经加载 可以用类名直接调用 比如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)非静态代码块 
[java] view plain copy
 
  1. public class TestStaticCon {     
  2.   
  3.    1.     public static int a = 0;     
  4.    2.     
  5.    3.     static {     
  6.    4.         a = 10;     
  7.    5.         System.out.println("父类的非静态代码块在执行a=" + a);     
  8.    6.     }     
  9.    7.          
  10.    8.     {     
  11.    9.         a = 8;     
  12.   10.         System.out.println("父类的非静态代码块在执行a=" + a);     
  13.   11.     }     
  14.   12.     
  15.   13.     public TestStaticCon() {     
  16.   14.         this("a在父类带参构造方法中的值:" + TestStaticCon.a); // 调用另外一个构造方法     
  17.   15.         System.out.println(a);     
  18.   16.         System.out.println("父类无参构造方法在执行a=" + a);     
  19.   17.     }     
  20.   18.     
  21.   19.     public TestStaticCon(String n) {     
  22.   20.         System.out.println(n);     
  23.   21.         System.out.println(a);     
  24.   22.     
  25.   23.     }     
  26.   24.     
  27.   25.     public static void main(String[] args) {     
  28.   26.         TestStaticCon tsc = null;     
  29.   27.         System.out.println("!!!!!!!!!!!!!!!!!!!!!");     
  30.   28.         tsc = new TestStaticCon();     
  31.   29.     }/*Output:   
  32.   30.            父类的非静态代码块在执行a=10   
  33.   31.            !!!!!!!!!!!!!!!!!!!!!   
  34.   32.            父类的非静态代码块在执行a=8   
  35.   33.            a在父类带参构造方法中的值:10   
  36.   34.            8   
  37.   35.            8   
  38.   36.            父类无参构造方法在执行a=8   
  39.   37.            */    
  40.   38.     
  41.   39. }    
  42.   
  43. public class TestStaticCon {     
  44.   
  45.    1.     public static int a = 0;     
  46.    2.     
  47.    3.     static {     
  48.    4.         a = 10;     
  49.    5.         System.out.println("父类的非静态代码块在执行a=" + a);     
  50.    6.     }     
  51.    7.          
  52.    8.     {     
  53.    9.         a = 8;     
  54.   10.         System.out.println("父类的非静态代码块在执行a=" + a);     
  55.   11.     }     
  56.   12.     
  57.   13.     public TestStaticCon() {     
  58.   14.         this("a在父类带参构造方法中的值:" + TestStaticCon.a); // 调用另外一个构造方法     
  59.   15.         System.out.println(a);     
  60.   16.         System.out.println("父类无参构造方法在执行a=" + a);     
  61.   17.     }     
  62.   18.     
  63.   19.     public TestStaticCon(String n) {     
  64.   20.         System.out.println(n);     
  65.   21.         System.out.println(a);     
  66.   22.     
  67.   23.     }     
  68.   24.     
  69.   25.     public static void main(String[] args) {     
  70.   26.         TestStaticCon tsc = null;     
  71.   27.         System.out.println("!!!!!!!!!!!!!!!!!!!!!");     
  72.   28.         tsc = new TestStaticCon();     
  73.   29.     }/*Output:   
  74.   30.            父类的非静态代码块在执行a=10   
  75.   31.            !!!!!!!!!!!!!!!!!!!!!   
  76.   32.            父类的非静态代码块在执行a=8   
  77.   33.            a在父类带参构造方法中的值:10   
  78.   34.            8   
  79.   35.            8   
  80.   36.            父类无参构造方法在执行a=8   
  81.   37.            */    
  82.   38.     
  83.   39. }    


[java] view plain copy
 
  1. public class TestStaticCon {  
  2.     public static int a = 0;  
  3.   
  4.     static {  
  5.         a = 10;  
  6.         System.out.println("父类的非静态代码块在执行a=" + a);  
  7.     }  
  8.       
  9.     {  
  10.         a = 8;  
  11.         System.out.println("父类的非静态代码块在执行a=" + a);  
  12.     }  
  13.   
  14.     public TestStaticCon() {  
  15.         this("a在父类带参构造方法中的值:" + TestStaticCon.a); // 调用另外一个构造方法  
  16.         System.out.println(a);  
  17.         System.out.println("父类无参构造方法在执行a=" + a);  
  18.     }  
  19.   
  20.     public TestStaticCon(String n) {  
  21.         System.out.println(n);  
  22.         System.out.println(a);  
  23.   
  24.     }  
  25.   
  26.     public static void main(String[] args) {  
  27.         TestStaticCon tsc = null;  
  28.         System.out.println("!!!!!!!!!!!!!!!!!!!!!");  
  29.         tsc = new TestStaticCon();  
  30.     }/*Output: 
  31.            父类的非静态代码块在执行a=10 
  32.            !!!!!!!!!!!!!!!!!!!!! 
  33.            父类的非静态代码块在执行a=8 
  34.            a在父类带参构造方法中的值:10 
  35.            8 
  36.            8 
  37.            父类无参构造方法在执行a=8 
  38.            */  
  39.   
  40. }  




 



原文地址:https://www.cnblogs.com/weixiuli/p/7054499.html