• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static code?

 
roul ravashimka
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I was wondering what the code at line 4 does? Does static mean that this code is always executed when an instance is made?
public class ServiceLocator {
private InitialContext ic;//1
private Map cache; //2
private static ServiceLocator me;//3
static {//4
try {
me = new ServiceLocator();
}
catch(ServiceLocatorException se) {
System.err.println(se);
se.printStackTrace(System.err);
}
}

private ServiceLocator() throws ServiceLocatorException {
try {
ic = new InitialContext();
cache = Collections.synchronizedMap(new HashMap());
}
catch (NamingException ne) {
throw new ServiceLocatorException(ne);
}
catch (Exception e) {
throw new ServiceLocatorException(e);
}
}

static public ServiceLocator getInstance() {
return me;
}
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a good explanation:
http://java.sun.com/developer/JDCTechTips/2004/tt0406.html#2
Brian
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic