Studyon Minte9.com
Head First (Java)




Constructor



* Default constructor

	A constructor does look and feel a lot like a method, but it's not a method. It's got the code that runs 
	when you instantiate an object. Every class you create has a constructor, even if you don't write it 
	yourself.

	%java

		package testingapp;
		public class TestingApp {
		    public static void main(String[] args) {
			Output o = new Output();

		    }
		}

		class Output{

		}


* No type

	Constructor has no return type.

	%java

		package testingapp;
		public class TestingApp {
		    public static void main(String[] args) {
			Output o = new Output("aaa");
			o.Output("bbb");
			    // Output: bbb
		    }
		}

		class Output{

		    public Output(String s) { // constructor (no return type)
		    }
		    
		    public void Output(String s) { // method (return type)
			System.out.println(s);
		    }
		}


* Overloaded constructors

	You could have more than one constructor in a class.

	%java

		package testingapp;
		public class TestingApp {
		    public static void main(String[] args) {
			Output o1 = new Output();
			Output o2 = new Output("aaa");
		    }
		}

		class Output{

		    public Output() { // constructor 1
		    }
		    
		    public Output(String s) { // constructor 2
		    }
		    
		    public void Output(String s) { // method (return type)
			System.out.println(s);
		    }
		}

* Inheritage

	The first statement of your constructor must be a call to superclass constructor. 
	
	%java

	    class CustomRenderer extends DefaultTableCellRenderer {
                String val;
                public CustomRenderer (String val) {
                   super();
                   this.col = col;
                }
		...
	}