JAVA静态代码块,静态方法,非静态方法 简单比较!~
2007-06-08 16:38
281 查看
静态代码块不包含在任何方法体中,当类被载入时自动执行静态代码块,且只被执行一次经,常用于类属性的初始化
静态方法 百度一下
StaticCode 方法如下
package org.dalong.staticblockcode;
/**
* @author HuangDaLong
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class StaticCode {
protected static String teststring;
static {
System.out.println("call staticcode successful!");
System.out.println("This is teststring: "+teststring );
}
protected static void staticMetho(){
System.out.println("hello,This is Static metho!");
teststring="Static metho made this string!";
}
public String getTeststring() {
return teststring;
}
public void setTeststring(String teststring) {
this.teststring = teststring;
}
}
测试方法如下,大家可以比较一下输出的结果;
package org.dalong.staticblockcode;
/**
* @author HuangDaLong
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class StaticCodeTest {
public static void main(String args[]) {
/**
* print:call staticcode successful!
* This is tststring:null
*/
new StaticCode();
/**
* no print;
*/
new StaticCode();
/**
* no print;
*/
StaticCode sc = new StaticCode();
/**
* print: null
*/
System.out.println(sc.getTeststring());
/**
* print: hello test;
*/
sc.setTeststring("hello,test!");
System.out.println(sc.getTeststring());
/**
* print:hello,This is Static metho!
*/
StaticCode.staticMetho();
/**
* print:Static metho made this string!
*/
System.out.println(sc.getTeststring());
}
}
静态方法 百度一下
StaticCode 方法如下
package org.dalong.staticblockcode;
/**
* @author HuangDaLong
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class StaticCode {
protected static String teststring;
static {
System.out.println("call staticcode successful!");
System.out.println("This is teststring: "+teststring );
}
protected static void staticMetho(){
System.out.println("hello,This is Static metho!");
teststring="Static metho made this string!";
}
public String getTeststring() {
return teststring;
}
public void setTeststring(String teststring) {
this.teststring = teststring;
}
}
测试方法如下,大家可以比较一下输出的结果;
package org.dalong.staticblockcode;
/**
* @author HuangDaLong
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class StaticCodeTest {
public static void main(String args[]) {
/**
* print:call staticcode successful!
* This is tststring:null
*/
new StaticCode();
/**
* no print;
*/
new StaticCode();
/**
* no print;
*/
StaticCode sc = new StaticCode();
/**
* print: null
*/
System.out.println(sc.getTeststring());
/**
* print: hello test;
*/
sc.setTeststring("hello,test!");
System.out.println(sc.getTeststring());
/**
* print:hello,This is Static metho!
*/
StaticCode.staticMetho();
/**
* print:Static metho made this string!
*/
System.out.println(sc.getTeststring());
}
}
相关文章推荐
- java之静态代码块、构造块、构造方法的简单例子
- java synchronized修饰普通方法,修饰静态方法,修饰代码块,修饰线程run方法 比较
- java synchronized修饰普通方法,修饰静态方法,修饰代码块,修饰线程run方法 比较
- java中静态的代码块,静态变量,静态方法
- 【转载】Java中静态的代码块,静态变量,静态方法
- synchronized修饰普通方法,修饰静态方法,修饰代码块,修饰线程run方法 比较
- Java中静态代码块,静态变量,静态方法,构造方法初始化顺序
- Java中非静态代码块,静态代码块和构造方法的初始化顺序
- java中的静态变量、静态方法与静态代码块区别
- java 静态代码块 静态方法区别
- Java中静态代码块与构造代码块的简单介绍
- Java 多线程:synchronized 关键字 3ff0 用法(修饰类,方法,静态方法,代码块)
- 深入理解Java中的同步静态方法和synchronized(class)代码块的类锁
- java中类的静态代码块、构造代码块、构造方法、静态成员的初始化顺序详解
- java中静态代码块的用法及其与静态方法的区别
- Java 多线程:synchronized 关键字用法(修饰类,方法,静态方法,代码块)
- 黑马程序员——Java基础---静态代码块和构造方法执行顺序
- java静态代码块、初始化块和构造方法的执行顺序
- java静态代码块、初始化块和构造方法的执行顺序
- java静态代码块、构造方法、构造块的执行顺序