Showing posts from December, 2011

Jar command

Jar is a java archive file.Its like a zipped file.

All the java files can be archived into a jar file using the jar command.

jar - cf  MyJar.jar Myapp.--> archives Myapp into a jar file named MyJar.jar

To list the files in the Jar file.

jar-tf MyJar.jar

Relative Paths and Absolute Path in Java

Relative paths is the one the do not start with a  forward slash(/) or a backward slash(\).Example for Relative path:  -classpath dirB: dirB/dirC.  The relative path is meaningfull only when the user current directory is directory A where B is its sub directory.Absolute paths start with a slash. Indicates the root directory.

Java Class Path

Java class path can be thought as class search paths.Classes are searched from left to right.
Example of classpath:  -classpath  /com/foo
Classpaths can be declared in two places:

   as an environment variable in the operating system   as a command line option
The classpath declared as an environment variable is a default option.The classpath declared as an systems environment variable is used by default whenever java or javac is invoked.

The classpath declared as an command line option is temporary and exists only for the length of invocation.

Java Reflection Examples

Java Reflection is used the extensively in following areas:

JUNIT JAR-Junit 3.0 uses reflection to find methods that start with" test".The algorithm was changed in 4.0.Now reflections are used to find methods with @ test annotations.Java Editors and IDE: Most of the java editors like eclipse,Netbeans and others use reflection to populate the methods associated with the class.For example in most of the java editors when you type "classname." and press space or tab you get all the methods associated with it.The method suggestion is shown using java refelection.Java Ant Build Files: A user can use taskdef property to instantiate and compile a user defined task.Spring framework:Spring uses reflection to create an Object of each Bean.The spring xml file has the classname attribute which is read to create bean objects.All the Bean Objects will be created using a default constructor and the proprties of the objects are examined.Runtime code generation:This is used by IDES …

Singleton class-Violation

private static MyObject myObject;
private MyObject (){
public static synchronized MyObject getInstanceObject(){

myObject=new Object();

return myObject

It could happen that the access method may be called twice from 2 different classes at the same time and hence more than one object being created. This could violate the design patter principle. In order to prevent the simultaneous invocation of the getter method by 2 threads or classes simultaneously we add the synchronized keyword to the method declaration

Singleton Class-Private Constructor

A class is called a singleton class, if it allows only one instance of the object to be created throughout the application.

A singleton class has the following

 private constructor which-prevent other classes from instantiating it directly.a public method with static modifier which returns the instance.--a static method can be accessed at class level without     creating an object.private static instance of the class

package singleotons;

Public class Logger{
private static Logger logger;

private Logger(){

public static Logger getLoggerInstance(){
if (logger==null){

logger=new Logger();
return logger;