Created
January 19, 2014 09:04
-
-
Save rajeevprasanna/8502254 to your computer and use it in GitHub Desktop.
constructor overloading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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