|
| 1 | +--- |
| 2 | +title: Java Constructors |
| 3 | +description: In this tutorial, we will learn how to use the Java constructors with examples. |
| 4 | +--- |
| 5 | + |
| 6 | +A constructor in Java is similar to a method that is invoked when an object of the class is created. |
| 7 | + |
| 8 | +Unlike Java methods, a constructor has the same name as that of the class and does not have any return type. For example, |
| 9 | + |
| 10 | +```java |
| 11 | +class Test { |
| 12 | + Test() { |
| 13 | + // constructor body |
| 14 | + } |
| 15 | +} |
| 16 | +``` |
| 17 | +Here, `Test()` is a constructor. It has the same name as that of the class and doesn't have a return type. |
| 18 | + |
| 19 | +## Example: Java Constructor |
| 20 | +```java |
| 21 | +class Main { |
| 22 | + private String name; |
| 23 | + |
| 24 | + // constructor |
| 25 | + Main() { |
| 26 | + System.out.println("Constructor Called:"); |
| 27 | + name = "Javaistic"; |
| 28 | + } |
| 29 | + |
| 30 | + public static void main(String[] args) { |
| 31 | + |
| 32 | + // constructor is invoked while |
| 33 | + // creating an object of the Main class |
| 34 | + Main obj = new Main(); |
| 35 | + System.out.println("The name is " + obj.name); |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | +### Output: |
| 40 | + |
| 41 | +```plaintext |
| 42 | +Constructor Called: |
| 43 | +The name is Javaistic |
| 44 | +``` |
| 45 | +In the above example, we have created a constructor named `Main()`. |
| 46 | + |
| 47 | +Inside the constructor, we are initializing the value of the `name` [variable](/docs/variables-literals). |
| 48 | + |
| 49 | +Notice the statement creating an object of the `Main` class. |
| 50 | + |
| 51 | +```java |
| 52 | +Main obj = new Main(); |
| 53 | +``` |
| 54 | +Here, when the object is created, the `Main()` constructor is called. And the value of the name variable is initialized. |
| 55 | + |
| 56 | +Hence, the program prints the value of the name variables as Javaistic. |
| 57 | + |
| 58 | +## Types of Constructor |
| 59 | +In Java, constructors can be divided into three types: |
| 60 | + |
| 61 | +- [No-Arg Constructor](#1-java-no-arg-constructors) |
| 62 | +- [Parameterized Constructor](#2-java-parameterized-constructor) |
| 63 | +- [Default Constructor](#3-java-default-constructor) |
| 64 | + |
| 65 | +## 1. Java No-Arg Constructors |
| 66 | + |
| 67 | +Similar to methods, a Java constructor may or may not have any parameters (arguments). |
| 68 | + |
| 69 | +If a constructor does not accept any parameters, it is known as a no-argument constructor. For example, |
| 70 | + |
| 71 | +```java |
| 72 | +private Constructor() { |
| 73 | + // body of the constructor |
| 74 | +} |
| 75 | +``` |
| 76 | +### Example: Java Private No-arg Constructor |
| 77 | +```java |
| 78 | +class Main { |
| 79 | + |
| 80 | + int i; |
| 81 | + |
| 82 | + // constructor with no parameter |
| 83 | + private Main() { |
| 84 | + i = 5; |
| 85 | + System.out.println("Constructor is called"); |
| 86 | + } |
| 87 | + |
| 88 | + public static void main(String[] args) { |
| 89 | + |
| 90 | + // calling the constructor without any parameter |
| 91 | + Main obj = new Main(); |
| 92 | + System.out.println("Value of i: " + obj.i); |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | +#### Output: |
| 97 | + |
| 98 | +```plaintext |
| 99 | +Constructor is called |
| 100 | +Value of i: 5 |
| 101 | +``` |
| 102 | +In the above example, we have created a constructor Main(). |
| 103 | +Here, the constructor does not accept any parameters. Hence, it is known as a no-arg constructor. |
| 104 | +Notice that we have declared the constructor as private. |
| 105 | +Once a constructor is declared private, it cannot be accessed from outside the class. |
| 106 | + |
| 107 | +So, creating objects from outside the class is prohibited using the private constructor. |
| 108 | +Here, we are creating the object inside the same class. |
| 109 | + |
| 110 | +Hence, the program is able to access the constructor. To learn more, visit Java Implement Private Constructor. |
| 111 | +However, if we want to create objects outside the class, then we need to declare the constructor as public. |
| 112 | + |
| 113 | +### Example: Java Public no-arg Constructors |
| 114 | +```java |
| 115 | +class Company { |
| 116 | + String name; |
| 117 | + |
| 118 | + // public constructor |
| 119 | + public Company() { |
| 120 | + name = "Javaistic"; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +class Main { |
| 125 | + public static void main(String[] args) { |
| 126 | + |
| 127 | + // object is created in another class |
| 128 | + Company obj = new Company(); |
| 129 | + System.out.println("Company name = " + obj.name); |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | +#### Output |
| 134 | + |
| 135 | +```plaintext |
| 136 | +Company name = Javaistic |
| 137 | +``` |
| 138 | +## 2. Java Parameterized Constructor |
| 139 | + |
| 140 | +A Java constructor can also accept one or more parameters. Such constructors are known as parameterized constructors (constructors with parameters). |
| 141 | + |
| 142 | +### Example: Parameterized Constructor |
| 143 | +```java |
| 144 | +class Main { |
| 145 | + |
| 146 | + String languages; |
| 147 | + |
| 148 | + // constructor accepting single value |
| 149 | + Main(String lang) { |
| 150 | + languages = lang; |
| 151 | + System.out.println(languages + " Programming Language"); |
| 152 | + } |
| 153 | + |
| 154 | + public static void main(String[] args) { |
| 155 | + |
| 156 | + // call constructor by passing a single value |
| 157 | + Main obj1 = new Main("Java"); |
| 158 | + Main obj2 = new Main("Python"); |
| 159 | + Main obj3 = new Main("C"); |
| 160 | + } |
| 161 | +} |
| 162 | +``` |
| 163 | +#### Output |
| 164 | + |
| 165 | +```plaintext |
| 166 | +Java Programming Language |
| 167 | +Python Programming Language |
| 168 | +C Programming Language |
| 169 | +``` |
| 170 | +In the above example, we have created a constructor named `Main()`. |
| 171 | +Here, the constructor takes a single parameter. Notice the expression: |
| 172 | + |
| 173 | +```java |
| 174 | +Main obj1 = new Main("Java"); |
| 175 | +``` |
| 176 | +Here, we are passing the single value to the constructor. |
| 177 | +Based on the argument passed, the language variable is initialized inside the constructor. |
| 178 | + |
| 179 | +## 3. Java Default Constructor |
| 180 | + |
| 181 | +If we do not create any constructor, the Java compiler automatically creates a no-arg constructor during the execution of the program. |
| 182 | +This constructor is called the default constructor. |
| 183 | + |
| 184 | +### Example: Default Constructor |
| 185 | +```java |
| 186 | +class Main { |
| 187 | + |
| 188 | + int a; |
| 189 | + boolean b; |
| 190 | + |
| 191 | + public static void main(String[] args) { |
| 192 | + |
| 193 | + // calls default constructor |
| 194 | + Main obj = new Main(); |
| 195 | + |
| 196 | + System.out.println("Default Value:"); |
| 197 | + System.out.println("a = " + obj.a); |
| 198 | + System.out.println("b = " + obj.b); |
| 199 | + } |
| 200 | +} |
| 201 | +``` |
| 202 | +#### Output |
| 203 | + |
| 204 | +```plaintext |
| 205 | +Default Value: |
| 206 | +a = 0 |
| 207 | +b = false |
| 208 | +``` |
| 209 | +Here, we haven't created any constructors. |
| 210 | + |
| 211 | +Hence, the Java compiler automatically creates the default constructor. |
| 212 | + |
| 213 | +The default constructor initializes any uninitialized instance variables with default values. |
| 214 | + |
| 215 | +| Type | Default | Value | |
| 216 | +|:-----:|:-------:|:-----:| |
| 217 | +| boolean | | false | |
| 218 | +| byte | | 0 | |
| 219 | +| short | | 0 | |
| 220 | +| int | | 0 | |
| 221 | +| long | | 0L | |
| 222 | +| char | | \u0000 | |
| 223 | +| float | | 0.0f | |
| 224 | +| double | | 0.0d | |
| 225 | +| object| Reference | null | |
| 226 | + |
| 227 | +To learn more, visit [Java Data Types](/docs/variables-primitive-data-types). |
| 228 | +In the above program, the variables a and b are initialized with default value `0` and `false` respectively. |
| 229 | + |
| 230 | +The above program is equivalent to: |
| 231 | + |
| 232 | +```java |
| 233 | +class Main { |
| 234 | + |
| 235 | + int a; |
| 236 | + boolean b; |
| 237 | + |
| 238 | + Main() { |
| 239 | + a = 0; |
| 240 | + b = false; |
| 241 | + } |
| 242 | + |
| 243 | + public static void main(String[] args) { |
| 244 | + // call the constructor |
| 245 | + Main obj = new Main(); |
| 246 | + |
| 247 | + System.out.println("Default Value:"); |
| 248 | + System.out.println("a = " + obj.a); |
| 249 | + System.out.println("b = " + obj.b); |
| 250 | + } |
| 251 | +} |
| 252 | +``` |
| 253 | +#### Output |
| 254 | + |
| 255 | +```plaintext |
| 256 | +Default Value: |
| 257 | +a = 0 |
| 258 | +b = false |
| 259 | +``` |
| 260 | +### Important Notes on Java Constructors |
| 261 | + |
| 262 | +Constructors are invoked implicitly when you instantiate objects. |
| 263 | + |
| 264 | +The two rules for creating a constructor are: |
| 265 | + |
| 266 | +1. The name of the constructor should be the same as the class. |
| 267 | +2. A Java constructor must not have a return type. |
| 268 | + |
| 269 | +If a class doesn't have a constructor, the Java compiler automatically creates a default constructor during run-time. The default constructor initializes instance variables with default values. For example, the int variable will be initialized to 0 |
| 270 | + |
| 271 | +### Constructor types: |
| 272 | +No-Arg Constructor - a constructor that does not accept any arguments |
| 273 | +Parameterized constructor - a constructor that accepts arguments |
| 274 | +Default Constructor - a constructor that is automatically created by the Java compiler if it is not explicitly defined. |
| 275 | +A constructor cannot be abstract or static or final. |
| 276 | +A constructor can be overloaded but can not be overridden. |
| 277 | + |
| 278 | +## Constructors Overloading in Java |
| 279 | +Similar to Java method overloading, we can also create two or more constructors with different parameters. This is called constructor overloading. |
| 280 | + |
| 281 | +### Example: Java Constructor Overloading |
| 282 | +```java |
| 283 | +class Main { |
| 284 | + |
| 285 | + String language; |
| 286 | + |
| 287 | + // constructor with no parameter |
| 288 | + Main() { |
| 289 | + this.language = "Java"; |
| 290 | + } |
| 291 | + |
| 292 | + // constructor with a single parameter |
| 293 | + Main(String language) { |
| 294 | + this.language = language; |
| 295 | + } |
| 296 | + |
| 297 | + public void getName() { |
| 298 | + System.out.println("Programming Language: " + this.language); |
| 299 | + } |
| 300 | + |
| 301 | + public static void main(String[] args) { |
| 302 | + |
| 303 | + // call constructor with no parameter |
| 304 | + Main obj1 = new Main(); |
| 305 | + |
| 306 | + // call constructor with a single parameter |
| 307 | + Main obj2 = new Main("Python"); |
| 308 | + |
| 309 | + obj1.getName(); |
| 310 | + obj2.getName(); |
| 311 | + } |
| 312 | +} |
| 313 | +``` |
| 314 | +#### Output |
| 315 | + |
| 316 | +```plaintext |
| 317 | +Programming Language: Java |
| 318 | +Programming Language: Python |
| 319 | +``` |
| 320 | +In the above example, we have two constructors: `Main()` and `Main(String language)`. |
| 321 | + |
| 322 | +Here, both the constructors initialize the value of the variable language with different values. |
| 323 | +Based on the parameter passed during object creation, different constructors are called, and different values are assigned. |
| 324 | +It is also possible to call one constructor from another constructor. To learn more, visit Java Call One Constructor from Another. |
| 325 | + |
| 326 | +<TipInfo> |
| 327 | + |
| 328 | +**Note:** We have used this keyword to specify the variable of the class. To know more about this keyword, visit [Java this keyword](/docs/this-keyword). |
| 329 | + |
| 330 | +</TipInfo> |
0 commit comments