Skip to content

Instantly share code, notes, and snippets.

@rajeevprasanna
Last active January 3, 2016 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajeevprasanna/8502002 to your computer and use it in GitHub Desktop.
Save rajeevprasanna/8502002 to your computer and use it in GitHub Desktop.
constructor chaining
private int constant;
private Bar(){
//System.out.println("tewsts");
//this(5);
System.out.println("default constructor is called");
}
private static boolean instanceAvailable;
private static Bar bar;
public static Bar getInstance() {
if (!instanceAvailable) {
bar = new Bar();
instanceAvailable = true;
}
return bar;
}
public void test(){
}
public static void main(String[] args){
//Bar b = new Bar(5);
//b.test();
// output :
//abstract Foo class constructor is invoked
//Bar constructor is invoked
}
package constructors.constructorChaining;
public abstract class Foo {
public Foo(){
System.out.println("abstract Foo class constructor is invoked");
}
public abstract void test();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment