• Post Reply Bookmark Topic Watch Topic
  • New Topic

two question about initialize object  RSS feed

 
leon ting
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()");
}
}
1.why can do like this:
static {
c1 = new Cup(1);
c2 = new Cup(2);
}
2.since c1,c2 are static in declaration, why still use static key word when intialize them?
 
C. Nimo
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
You use the static { } block even though the Cup objects are static because you want their initialization to happen once, before you have an instance of your wrapping class up and running.
Nimo.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!