-
Notifications
You must be signed in to change notification settings - Fork 0
/
constructors.html
173 lines (135 loc) · 6.05 KB
/
constructors.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!--
~ Author: @tridib2003
~ Repository: https://github.com/tridib2003/levelupjava
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Constructors | Level-Up Java</title>
<link href="styles.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<nav class="navigation-std">
<ul class="ul-remove-bullet nav-pills">
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Previous</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Home</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Next</a>
</li>
</ul>
</nav>
<h1 class="h1-basic">Constructors</h1>
<div class="desc-container desc-container-center desc-std">
<hr>
<ul>
<li>
<p>
A <strong>constructor</strong> initializes an object immediately upon creation, which make an object immediately usable upon its creation.
</p>
</li>
<li>
<p>
It has the same name as the class in which it is defined and is automatically called when an object of the class is created, before the <strong>new</strong> operator completes.
</p>
</li>
<li>
<p>
Constructors do not have any return type, not even <strong>void</strong>.
</p>
</li>
<li>
<p>
While allocating space in memory for an object, we use parentheses after the class name immediately after the <strong>new</strong> keyword, which essentially calls the constructor for the class.
</p>
</li>
<li>
<p>
When a constructor is not explicitly defined for a class, a <strong>default constructor</strong> is created. This default constructor sets all non-initialized variables to their default values. <strong>0</strong> for numeric, <strong>false</strong> for boolean, and <strong>null</strong> for reference types.
</p>
</li>
<li>
<p>
Once the user defines her own version of the constructor the default constructor is no longer used.
</p>
</li>
<li>
<p>
<strong>Parameterized constructors</strong> helps to initialize each object with the specified parameters at the time of object creation.
</p>
</li>
<li>
<p>
<strong>this</strong> keyword is used to refer to the current object, i.e. it serves as a reference to the object on which the method was invoked. Sometimes the local variable has the same name as an instance variable, in such cases the local variable hides the instance variable. To deal with such cases, we can use <strong>this</strong> keyword that refer directly to the object, i.e. <strong>this</strong> can be used to resolve any namespace collisions that might occur between instance variables and local variables.
</p>
</li>
<li>
<p>
We can use <strong>this</strong> keyword followed by the <strong>dot (.)</strong> operator and then the instance variable name, to access the instance variable of the current object having the same name as the one in formal parameter.
</p>
</li>
</ul>
</div>
<br><br><br>
<div class="table-container table-container-center">
<table class="table-code-links">
<tr>
<td>
Initialization using constructor
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Constructors/EmployeeConstDemo.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
<tr>
<td>
Using parameterized constructor
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Constructors/ParameterizedConstructors.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
</table>
</div>
<br><br><br>
<footer class="navigation-std">
<ul class="ul-remove-bullet nav-pills">
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Previous</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Home</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Next</a>
</li>
</ul>
</footer>
</body>
</html>