public class Count {
public static void prt(String s){
System.out.println(s);
}
Value v=new Value(10);
static Value v1,v2;
static{
prt("v1.c="+v1.c+" v2.c="+v2.c);
v1=new Value(27);
prt("v1.c="+v1.c+" v2.c="+v2.c);
v2=new Value(15);
prt("v1.c="+v1.c+" v2.c="+v2.c);
} public static void main(String[] args){
Count ct=new Count();
prt("ct.c="+ct.v.c);
prt("v1.c="+v1.c+" v2.c="+v2.c);
v1.inc();
prt("v1.c="+v1.c+" v2.c="+v2.c);
prt("ct.c="+ct.v.c);
}
}
class Value{
static int c=1;
Value(){
c=15;
}
Value(int i){
c=i;
}
static void inc(){
c++;
}
}
public static void prt(String s){
System.out.println(s);
}
Value v=new Value(10);
static Value v1,v2;
static{
prt("v1.c="+v1.c+" v2.c="+v2.c);
v1=new Value(27);
prt("v1.c="+v1.c+" v2.c="+v2.c);
v2=new Value(15);
prt("v1.c="+v1.c+" v2.c="+v2.c);
} public static void main(String[] args){
Count ct=new Count();
prt("ct.c="+ct.v.c);
prt("v1.c="+v1.c+" v2.c="+v2.c);
v1.inc();
prt("v1.c="+v1.c+" v2.c="+v2.c);
prt("ct.c="+ct.v.c);
}
}
class Value{
static int c=1;
Value(){
c=15;
}
Value(int i){
c=i;
}
static void inc(){
c++;
}
}
解决方案 »
- Heritrix配置后,不能抓取,请求帮助
- 问一个名词解释
- 字符串的简单问题,请大家帮忙
- mysql 主键插入数据问题???
- 怎么回事儿,急!
- Socket客户端为什么Thread没有效果?
- 请问大家一个低级的问题:在windows里显示对话框的函数是AfxMessageBox("OK"),在纯java里怎么用,还要引用什么包吗?
- 我昨天刚学java用的是JBuilder可是我用F9运行之后在classes目录中生成的.class文件为什么我在用java.exe application1时出现如下的错误呢
- 有关VJ++编译后EXE程序的奇怪问题?
- 这个注解什么意思?
- 关于java拖动对话框的标题栏的问题,实现代码在哪里?
- 在线问一个java问题,一个字符传怎么知道包含在另一个字符传里面。。。。???给出方法者给分
* 创建日期 2005-8-1
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*//**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class Count {
public static void prt(String s) {
System.out.println(s);
} Value v = new Value(10); static Value v1, v2; static {
prt("v1.c=" + v1.c + " v2.c=" + v2.c);
v1 = new Value(27);
prt("v1.c=" + v1.c + " v2.c=" + v2.c);
v2 = new Value(15);
prt("v1.c=" + v1.c + " v2.c=" + v2.c);
} public static void main(String[] args) {
Count ct = new Count(); prt("ct.c=" + ct.v.c);
prt("v1.c=" + v1.c + " v2.c=" + v2.c);
v1.inc();
prt("v1.c=" + v1.c + " v2.c=" + v2.c);
prt("ct.c=" + ct.v.c);
}
}class Value {
static int c = 0; Value() {
c = 15;
} Value(int i) {
c = i;
} static void inc() {
c++;
}
}
v1.c=27 v2.c=27
v1.c=15 v2.c=15
ct.c=10
v1.c=10 v2.c=10
v1.c=11 v2.c=11
ct.c=11看了这个你就明白了 !
The order of initialization is statics first, if they haven’t already been initialized by a previous object creation, and then the non-static objects. You can see the evidence of this in the output.
It’s helpful to summarize the process of creating an object. Consider a class called Dog: The first time an object of type Dog is created (the constructor is actually a static method), or the first time a static method or static field of class Dog is accessed, the Java interpreter must locate Dog.class, which it does by searching through the classpath. As Dog.class is loaded (creating a Class object, which you’ll learn about later), all of its static initializers are run. Thus, static initialization takes place only once, as the Class object is loaded for the first time. When you create a new Dog( ), the construction process for a Dog object first allocates enough storage for a Dog object on the heap.This storage is wiped to zero, automatically setting all the primitives in that Dog object to their default values (zero for numbers and the equivalent for boolean and char) and the references to null. Any initializations that occur at the point of field definition are executed. Feedback
Constructors are executed. As you shall see in Chapter 6, this might actually involve a fair amount of activity, especially when inheritance is involved. Feedback
v1.c=27 v2.c=27
v1.c=15 v2.c=15
ct.c=10
v1.c=10 v2.c=10
v1.c=11 v2.c=11
ct.c=11