Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Understanding the Interplay Between Utility Classes and Static Initialization

Java is an OO language, which means much of the functionality of a Java application is encapsulated into cohesive classes that can be instantiated and acted upon.

Tutorial Details:

Nevertheless, once in a while you end up with some functions that are applicable to more than one class. These functions don't really belong to any particular class, but to a sub-system or a package. Although one can express this grouping as a class by itself (represented by interfaces), it is just simpler to collect them as static functions in a class, when one doesn't need the sophistication of service-centric approach for these methods. For example, I have a class called ServletUtils (see Example 1 below).

One characteristic of static functions is that they are stateless, although for efficiency they may depend on some static variables. When these static variables are introduced or utilized, you need to know the class initialization rules laid out by the language. Although these rules are fairly intuitive, it is beneficial to know them in some depth, as static initialization involves side effects. This article is about what these side effects are and how to minimize them through a pattern called Static Resource Holder. With this in mind, I want to first review the basics of static initialization rules. For a complete treatment of the rules, refer to the Java language specification links at the end of the article.

Example 1. ServletUtils

public class ServletUtils
{
static public void
writeOutAsJavaScript(PrintWriter out,
StringBuffer javaScript);

static public String
convertToHtmlLines(String inString);

static public Map
parseQueryString(String httpQueryString);

static public Map
getParameters(HttpServletRequest request);

static public String
getSusbstitutedURL(String encodedString,
Map arguments);

//.. so on and so forth
}


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Understanding the Interplay Between Utility Classes and Static Initialization

View Tutorial:
Understanding the Interplay Between Utility Classes and Static Initialization

Related Tutorials:

Programming Java threads in the real world, Part 3 - JavaWorld - November 1998
Programming Java threads in the real world, Part 3 - JavaWorld - November 1998
 
How to easily reconfigure your applications -- while they're running - JavaWorld - April 1999
How to easily reconfigure your applications -- while they're running - JavaWorld - April 1999
 
How to write a Java Card applet: A developer's guide
How to write a Java Card applet: A developer's guide
 
Understanding Java Card 2.0 - JavaWorld March 1998
Understanding Java Card 2.0 - JavaWorld March 1998
 
Smart object-management saves the day - JavaWorld November 1999
Smart object-management saves the day - JavaWorld November 1999
 
Understanding JavaServer Pages Model 2 architecture - JavaWorld December 1999
Understanding JavaServer Pages Model 2 architecture - JavaWorld December 1999
 
C#: A language alternative or just J--?, Part 2 - JavaWorld December 2000
C#: A language alternative or just J--?, Part 2 - JavaWorld December 2000
 
Make an EJB from any Java class with Java Reflection - JavaWorld December 2000
Make an EJB from any Java class with Java Reflection - JavaWorld December 2000
 
Object mobility in the Jini environment - JavaWorld January 2001
Object mobility in the Jini environment - JavaWorld January 2001
 
Device programming with MIDP, Part 1 - JavaWorld January 2001
Device programming with MIDP, Part 1 - JavaWorld January 2001
 
When is a Singleton not a Singleton? - JavaWorld January 2001
When is a Singleton not a Singleton? - JavaWorld January 2001
 
Encapsulation is not information hiding - JavaWorld May 2001
Encapsulation is not information hiding - JavaWorld May 2001
 
Can ThreadLocal solve the double-checked locking problem?
Can ThreadLocal solve the double-checked locking problem?
 
Implement a J2EE-aware application console in Swing
Implement a J2EE-aware application console in Swing
 
JSP best practices
Follow these tips for reusable and easily maintainable JavaServer Pages
 
Repair invalid cached services in the Service Locator pattern
Repair invalid cached services in the Service Locator pattern
 
Unwrap the package statement's potential
Unwrap the package statement's potential
 
Very interesting
Very interesting
 
Eye Of Newt - LDAP Editor
Eye Of Newt - LDAP Editor
 
Understanding the Interplay Between Utility Classes and Static Initialization
Java is an OO language, which means much of the functionality of a Java application is encapsulated into cohesive classes that can be instantiated and acted upon.
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.