public class StaticTest{
static{
System.out.println("Hi there");
}
public void print(){
System.out.println("Hello");
}
public static void main (String[] args) {
StaticTest st1=new StaticTest();
st1.print();
StaticTest st2=new StaticTest();
st2.print();
}
}
为什么会输出:Hi there 啊
static{
System.out.println("Hi there");
}
public void print(){
System.out.println("Hello");
}
public static void main (String[] args) {
StaticTest st1=new StaticTest();
st1.print();
StaticTest st2=new StaticTest();
st2.print();
}
}
为什么会输出:Hi there 啊
解决方案 »
- 软件工程,在真实项目中的运用
- java过滤器filter能过滤某个包下的所有servlet吗?
- 笔试题 : 编写一个JAVA类,用于计算两个日期之间的周数。
- struts2一次请求执行action里的二个方法
- hibernate自动建表无错误提示,却不见表
- 一直无法理解这段代码(获取数据集),请帮忙
- 请教高手:Hibernate如何解决动态表的持久化问题?
- 高分求解,不够再加,谢谢!
- 谁用过jdeveloper,我想请教各位怎么配置其中的application server connection?
- JAVA,J2EE配置问题,http://localhost:8000 毫无反应
- 对EasyMock.anyObject(对象.class) 中的对象设置值
- 怎样配置spring事务的隔离级别?????????????
html
System.out.println("Hi there");
}
有方法这样写的吗?是不是写的本身有问题?
static变量,是在 内存刚加载程序的时候,static就已经被实例化。即:有一段内存是装载 系统代码 用的,没有被任何其他应用实例化过。但是这段没有被实例化的程序,自己就开辟内存,为static变量进行实例化。
而且这段被实例化的static变量,将不能被其他代码实例化,所以他就是唯一的。唯一存在于装 系统代码 的那段内存当中。能不能这么理解?如果有不对的地方,请指出来!