Tuesday, April 8, 2008

Keywords not used in Java

  • goto
  • const


The keywords const and goto are reserved, even though they are not currently used. true, false, and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs.

Newly added keyword in Java 5 - enum

Refer

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html

Monday, April 7, 2008

How to prevent subclassing!

1. By declaring class as final

public final class Parent{
}


2. By having only private constructors. Super class constructors should be visible in order to subclass.

private Parent(){
}

Sunday, April 6, 2008

How to call blocks from other classes?

Non static blocks & Static blocks are executed one by one by the order of their defintion before any constructor is being executed.

Static blocks can also be invoked without creating instance of the Class(without actually calling constructors) by simply loading the Class.


Class.forName("ClassThatContainsStaticBlock");




class ExOne{

//Static block
static{
System.out.println("This is static block of ExOne");
}

//Non static block
{
System.out.println("This is non-static block of ExOne");
}

public ExOne(){
System.out.println("This is Constructor block of ExOne");

}
}

public class ExOneTest{

public static void main(String[] args) {
try {
// Static blocks can be called either by creating
// new instance or loading the class
new ExOne();
Class.forName("ExOne");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}

How to make a class immutable?

1)All of its fields are final
2)The class is declared final
3)The this reference is not allowed to escape during construction
4)Any fields that contain references to mutable objects, such as arrays, collections, or mutable classes like Date:
-Are private
-Are never returned or otherwise exposed to callers
-Are the only reference to the objects that they reference
-Do not change the state of the referenced objects after construction

Refer java.lang.String

How to redirect standard output/err to a file?



PrintStream err=null;
PrintStream out=null;
String outputFile="C://output.txt";
String errorFile="C://error.txt";

err=new PrintStream(new FileOutputStream(errorFile));
System.setErr(err);

//append all outputs
out=new PrintStream(new FileOutputStream(outputFile,true));
System.setOut(out);

System.err.print("This is error written to file");
System.out.print("This is output written to file");

Tuesday, April 1, 2008

Singleton pattern in Java

Simplest way to create a singleton instance is to use - static member/members, final class and private constructor.



class final Singleton{

private Singleton(){

}

static public doSomething(){
//do stuff here
}

}

class Client{

public static void main(String[] a){

Singleton.doSomething();

}

}



So wats wrong here?

a. Since Singleton class is final - cant subclass
b. Since Singleton class can effectively have only static members- it cannot implement any interface -very bad
c. all clients should access the Singleton with classname- hard binding
d. modifying existing pojo as singleton is nightmare of changing all over the places

Wat to do then?

Decouple the singleton functionality from singleton class with wrapper class.



class final SingletonWrapper{

static private Singleton instance_;

static synchronized public Singleton getInstance(){

if (instance_==null){
instance_=new Singleton();

}

return instance_;
}

}



Good article, why static singleton can't be a complete solution


http://radio.weblogs.com/0122027/stories/2003/10/20/implementingTheSingletonPatternInJava.html

Some more better models


/*
* Simple Traditional Way An instance is available whether used or not thread
* safe
*/
class AClass {

private final static AClass instance_ = new AClass();

private AClass() {
}

static AClass getInstance() {
return instance_;
}

void doStuff() {

}
}

/*
* New Java 5 way - use of volatile Instance is created with thread safe way
* when the fist getInstance is called
*/

class BClass {

private static volatile BClass instance_;

private BClass() {

}

static BClass getInstance() {

if (instance_ == null) {

synchronized (BClass.class) {

if (instance_ == null) {
instance_ = new BClass();
}
}
}

return instance_;
}

}

/*
* Better way to make use of Wrapper/Holder Lazy loaded when getInstance is
* called Thread safe
*/

class CClass {

private CClass() {

}

private static class CClassHolder {
private static final CClass instance = new CClass();
}

static CClass getInstance() {
return CClassHolder.instance;
}
}


Identify Duplicate Records


SELECT *
FROM my_org a
WHERE (SELECT COUNT (*)
FROM my_org b
WHERE a.legal_name=b.legal_name
and a.org_id <> b.org_id) > 1