Static variable
- A static variable is associated with its class rather than its object; hence they are known as class variables.
- A static variable is initialized only once when execution of the program starts.
- A static variable shares its state with all instances of the class.
- You access a static variable using its class name (instead of an instance).
- A static method can only access static variables and can call only static methods. In contrast, an instance method (non-static) may call a static method or access a static variable.
Counter.java
// Counter class should count the number of instances created from that class
public class Counter {
private static int count; // variable to store the number of objects created
// for every Counter object created, the default constructor will be called;
// so, update the counter value inside the default constructor
public Counter() {
count++;
}
public static void printCount() { // method to print the counter value so far
System.out.println("Number of instances created so far is: " + count);
}
public static void main(String []args) {
Counter anInstance = new Counter();
// note how we call printCount using the class name instead of instance variable name
Counter.printCount();
Counter anotherInstance = new Counter();
Counter.printCount();
}
}
Static Block
- This static block will be executed by JVM when it loads the class into memory.
- Do not confuse a static block with a constructor. A constructor will be invoked when an instance of the class is created, while the static block will be invoked when the program initializes.
public class SampleClass implements SampleInterface {
private static final Logger logger = SampleLogManager.getLogger(SampleClass .class, SampleLogManager.SampleProject.SampleModule);
private static Properties jdbcOperations = new Properties();
static {
InputStream is = null;
String configPropertyLocation = "/jdbc/sampleJDBC.properties";
try {
is = SampleClass.class.getResourceAsStream(configPropertyLocation) ;
jdbcOperations.load(is);
}
catch(Exception ex){
logger.error("Error in loading the resource stream for jdbc Operat ions"+ ex.getMessage(),ex);
}
finally {
if(is != null){
try { is.close(); } catch(Exception ex){
logger.error("Error in closing the resource stream for jdbc Operations"+ ex.getMessage(),ex);
}
}
}
}
private SampleVo2 findbyID(final SampleVo1 sampleVo) throws BusinessException {
// get the sql query from properties
String sql = jdbcOperations.getProperty("SELECT_CUSTOMER_BY_ID");
// do some sql operation
return sampleVo2;
}
Example 2: to establish spring bean initialization
public class TestCustomer extends BaseUnitTest {
private static String[] contexts = {"test-module-spring/test-module-applicationContext.xml"
};
private static CustomerService customerService = null;
static {
try {
ContextSingleton.getFactory(contexts);
customerService = (CustomerService)ContextSingleton.getFactory().getBean("customerService");
assertNotNull("CustomerService is null:", CustomerService);
} catch (Exception e) {
String errorMessage = "Unable to establish bean initialization: " + e.getMessage();
System.out.println(errorMessage);
fail(errorMessage);
}
}
// do something with CustomerService bean
}
No comments:
Post a Comment