File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes static code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "static code?" Watch "static code?" New topic

static code?

roul ravashimka
Ranch Hand

Joined: Mar 16, 2004
Posts: 53
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) {

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;

MSc Electronics, ICT
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
Here is a good explanation:

My Java Freeware:<br />MACCC - <a href="" target="_blank" rel="nofollow"></a><br />Nator - <a href="" target="_blank" rel="nofollow"></a>
I agree. Here's the link:
subject: static code?
It's not a secret anymore!