Skip to content

Instantly share code, notes, and snippets.

@iande
Created April 17, 2012 14:36
Show Gist options
  • Save iande/2406379 to your computer and use it in GitHub Desktop.
Save iande/2406379 to your computer and use it in GitHub Desktop.
When there is no explicit constructor...
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.
}
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.
}
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