Created
April 17, 2012 14:36
-
-
Save iande/2406379 to your computer and use it in GitHub Desktop.
When there is no explicit constructor...
This file contains hidden or 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
public static void main() { | |
// Java provides a "default" no-argument constructor for us. | |
MyCoolShit coolShit = new MyCoolShit(); | |
// But, NEVER rely on it! | |
System.out.println("Bool: " + coolShit.aBool); | |
System.out.println("Int: " + coolShit.anInt); | |
System.out.println("Double: " + coolShit.aDouble); | |
System.out.println("Char: " + coolShit.aChar); | |
System.out.println("String: " + coolShit.aString); | |
System.out.println("Rectangle: " + coolShit.aRectangle); | |
// This, however, will throw an exception: | |
MyLessCoolShit otherShit = new MyLessCoolShit(); | |
// because once we have defined any constructor, the JVM no | |
// longer provides the "default" one. | |
} |
This file contains hidden or 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
public class MyCoolShit { | |
public bool aBool; | |
public int anInt; | |
public double aDouble; | |
public char aChar; | |
public String aString; | |
public Rectangle aRectangle; | |
// Note: Our class has no explicit constructor. | |
} |
This file contains hidden or 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
public class MyLessCoolShit extends MyCoolShit { | |
public MyLessCoolShit(String str) { | |
super(); | |
this.aString = str; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment