@@ -37,7 +37,7 @@ as `12`.
3737### Below are the important rules for Constructors:
3838
3939__ 1.__ If you don't type a constructor into your class code, a default constructor will be automatically generated by
40- the compiler. The default constructor is ALWAYS a no-arg constructor .
40+ the compiler. The __ default constructor (the one supplied by compiler) is ALWAYS a no-arg constructor __ .
4141
4242<table >
4343
@@ -72,30 +72,40 @@ class Foo {
7272</tr >
7373
7474
75+ </table >
76+
77+ __ 2.__ Every constructor has, as its first statement, either a __ call to an overloaded constructor (` this() ` ) or a call to the
78+ superclass constructor (` super() ` )__ , and if not it is inserted by the compiler. Remember, compiler __ always inserts a _ no-arg_
79+ call to ` super() ` __ , it never passes any arguments.
80+
81+
82+ <table >
83+
7584<tr >
7685<td >
7786{% highlight java %}
78- public class Foo {
79-
87+ class Foo {
88+ Foo() { }
8089}
8190{% endhighlight %}
8291</td >
8392<td >
8493{% highlight java %}
85- public class Foo {
86- public Foo() {
94+ class Foo {
95+ Foo() {
8796 super();
8897 }
8998}
9099{% endhighlight %}
91100</td >
92101</tr >
93102
94-
95103</table >
96104
97- __ 2.__ Every constructor has, as its first statement, either a call to an overloaded constructor (` this() ` ) or a call to the
98- superclass constructor (` super() ` ), and if not it is inserted by the compiler.
105+
106+ __ 3.__ If you want a no-arg constructor and you've typed any other constructor(s) into your class code, the __ compiler won't
107+ provide the no-arg constructor (or any other constructor)__ for you. In other words, if you've typed in a constructor with
108+ arguments, you won't have a no-arg constructor unless you type it in yourself.
99109
100110
101111<table >
@@ -104,25 +114,66 @@ superclass constructor (`super()`), and if not it is inserted by the compiler.
104114<td >
105115{% highlight java %}
106116class Foo {
107- Foo() { }
117+ Foo(String s) {
118+ super();
119+ }
108120}
109121{% endhighlight %}
110122</td >
111123<td >
124+ Code is the same. Compiler doesn’t insert anything here.
125+ </td >
126+ </tr >
127+
128+ </table >
129+
130+ __ 4.__ Constructors can use __ any access modifier, including private__ . (A private constructor means only code within the
131+ class itself can instantiate an object of that type, so if the private constructor class wants to allow an instance of
132+ the class to be used, the class must provide a static method or variable that allows access to an instance created
133+ from within the class. Moreover, for a Singleton you are bound to make all constructors of class private.)
134+
135+ <table >
136+
137+ <tr >
138+ <td >
112139{% highlight java %}
113- class Foo {
114- Foo() {
140+ public class Foo {
141+
142+ }
143+ {% endhighlight %}
144+ </td >
145+ <td >
146+ {% highlight java %}
147+ public class Foo {
148+ public Foo() {
115149 super();
116150 }
117151}
118152{% endhighlight %}
119153</td >
120154</tr >
121155
122- </table >
123156
124- __ 3.__
157+ <tr >
158+ <td >
159+ {% highlight java %}
160+ private class Foo {
161+
162+ }
163+ {% endhighlight %}
164+ </td >
165+ <td >
166+ {% highlight java %}
167+ private class Foo {
168+ private Foo() {
169+ super();
170+ }
171+ }
172+ {% endhighlight %}
173+ </td >
174+ </tr >
125175
176+ </table >
126177
127178{% include responsive_ad.html %}
128179
0 commit comments