• Post Reply Bookmark Topic Watch Topic
  • New Topic

Base class  RSS feed

 
lavanya Ananth
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a base class. I want to create an intial context and also want to configure the log file. Should I place them in the constructor? Is this a good practice?

properties = new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
context = new InitialContext(properties);
String logConfigFile = getProperties("LOG_CONFIG_FILE");
System.out.println("Log Configuration File " + logConfigFile);
PropertyConfigurator.configure(logConfigFile);
}
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor should do just what it's name suggests it should do, ie construct an instance of the class. This means setting up everything that needs to be set up for the object to be in a meaningful state (but not neccessarily a useful state). However, if your class design is bad you will find you are adding loads of extra bits to your constructor that really shouldn't be there.

So to answer your question it really depends on what you are trying to do, for example, if your class is a logging class then configuring the log in the constructor is probably a good thing but if your class just logs out some of it's results then it probably should be created by another means, maybe by the creating class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!