Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday, 19 October 2012

Signleton Design Pattern in Java

A singleton is a class that is instantiated only once. This is typically accomplished by creating a static field in the class representing the class. A static method exists on the class to obtain the instance of the class and is typically named something such as getInstance(). The creation of the object referenced by the static field can be done either when the class is initialized or the first time that getInstance() is called. The singleton class typically has a private constructor to prevent the singleton class from being instantiated via a constructor. Rather, the instance of the singleton is obtained via the static getInstance() method.
The SingletonExample class is an example of a typical singleton class. It contains a private static SingletonExample field. It has a private constructor so that the class can't be instantiated by outside classes. It has a public static getInstance() method that returns the one and only SingletonExample instance. If this instance doesn't already exist, the getInstance() method creates it. The SingletonExample class has a public sayHello() method that can be used to test the singleton.

SingletonExample.java

package com.cakes;

public class SingletonExample {

 private static SingletonExample singletonExample = null;

 private SingletonExample() {
 }

 public static SingletonExample getInstance() {
  if (singletonExample == null) {
   singletonExample = new SingletonExample();
  }
  return singletonExample;
 }

 public void sayHello() {
  System.out.println("Hello");
 }
}
The Demo class obtains a SingletonExample singleton class via the call to the static SingletonExample.getInstance(). We call the sayHello() method on the singleton class. Executing the Demo class outputs "Hello" to standard output.

Demo.java

package com.cakes;

public class Demo {

 public static void main(String[] args) {
  SingletonExample singletonExample = SingletonExample.getInstance();

  singletonExample.sayHello();
 }

}
Singleton classes are a useful way of concentrating access to particular resources into a single class instance.

Solr Indexing using Java ??

Coming soon

Friday, 12 October 2012

Setting/Increase JVM heap size on Eclipse

java programs executes in JVM uses Heap of memory to manage the data. If your Java program requires a large amount of memory, it is possible that the virtual machine will begin to throw OutOfMemoryError instances when attempting to instantiate an object. The default heap size if 1 MB and can increase as much as 16 MB.

Setting/Increase JVM heap size on Eclipse

Right click on the main program -

run-configuration->-ArgumentTab->Inside VM   and insert the paramter.
-Xms<size>        set initial Java heap size
-Xmx<size>        set maximum Java heap size
such as  
-Xms 510 -Xmx 2048

--click on on apply and run the program.

Thanks

Wednesday, 19 September 2012

Display Hindi code in using java from Unicode.

import org.apache.commons.lang3.StringEscapeUtils;
// open the file as ASCII, read it into a string, then
String escapedStr; // = "\u0905\u092d\u0940 \u0938\u092e\u092f \u0939\u0948 ..."
// (to include such a string in a Java program you would have to double each \)
String hindiStr = StringEscapeUtils.unescapeJava( escapedStr );
System.out.println(hindiStr);