• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

static code?  RSS feed

 
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;
}
 
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
Boost this thread!