Skip to content

Instantly share code, notes, and snippets.

@rajeevprasanna
Created January 19, 2014 09:04
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/8502254 to your computer and use it in GitHub Desktop.
Save rajeevprasanna/8502254 to your computer and use it in GitHub Desktop.
constructor overloading
package constructors.overload;
public class Animal {
String name;
Animal(String name) {
System.out.println("constructor with name arg is invoked");
this.name = name;
}
Animal() {
//System.out.println("this statement is not allowed here.");
this(makeRandomName());
}
static String makeRandomName() {
int x = (int) (Math.random() * 5);
String name = new String[] { "Fluffy", "Fido", "Rover", "Spike", "Gigi" }[x];
return name;
}
public static void main(String[] args) {
Animal a = new Animal();
System.out.println(a.name);
Animal b = new Animal("Zeus");
System.out.println(b.name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment