????????????????????JAVA????????????????JAVA????????????????????????顣????e?????????????????????????????
???????????static???????????????
package com.test;
/**
* Created with IntelliJ IDEA.
* User: ???????
* Date: 14-6-15
* Time: ????11:19
* To change this template use File | Settings | File Templates.
*/
class Mug {
Mug(int marker) {
System.out.println("Mug(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}
public class Mugs {
Mug c1;
Mug c2;
{
c1 = new Mug(1);
c2 = new Mug(2);
System.out.println("c1 & c2 initialized");
}
Mugs() {
System.out.println("Mugs()");
}
public static void main(String[] args) {
System.out.println("Inside main()");
Mugs x = new Mugs();
Mugs y = new Mugs();
}
}
????????CODE??????
????snippet_file_0.txt
???????н?????????£?
????Inside main()
????Mug(1)
????Mug(2)
????c1 & c2 initialized
????Mugs()
????Mug(1)
????Mug(2)
????c1 & c2 initialized
????Mugs()
?????????????static?????????????
package com.test;
/**
* Created with IntelliJ IDEA.
* User: ???????
* Date: 14-6-15
* Time: ????11:21
* To change this template use File | Settings | File Templates.
*/
class Cup {
Cup(int marker) {
System.out.println("Cup(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}
class Cups {
// ??????????????????????????????????????
static Cup c1;
static Cup c2;
// ???????????α????????????
static {
c1 = new Cup(1);
c2 = new Cup(2);
}
Cups() {
System.out.println("Cups()");
}
}
class ExplicitStatic {
public static void main(String[] args) {
System.out.println("Inside main()");
Cups.c1.f(99); // (1)
}
// ?????????????
static Cups x = new Cups(); // (2)
// ????γ???????????Cups
static Cups y = new Cups(); // (2)
}
????????CODE??????
????snippet_file_0.txt
???????н?????????£?
????Cup(1)
????Cup(2)
????Cups()
????Cups()
????Inside main()
????f(99)
????????????????漰????????????д???顢?????????static?????????????????????????о??????????????
?????????????????static??????????????????????????????Σ??????????α???????????г?????????????????new????Σ?????????г??????