0

What if I use void in constructor? (Java)

Eg: class a { public void a{ } }

23rd Aug 2020, 5:01 AM
Ray
Ray - avatar
2 ответов
+ 2
If you make a "constructor" giving it a return type (in this case void) it will stop to be a constructor and will be a simple function, so when you create an object the code inside that "void constructor" isn't going to run BUT you can call it like a normal function using: Object.ObjectClass(); Have a nice day 🧏🏼‍♀️🧏🏼‍♀️🧏🏼‍♀️
23rd Aug 2020, 5:08 AM
Ariadne Rangel
Ariadne Rangel - avatar
+ 4
Then it will no longer be a constructor! Though your code will not show any error but that will turn into a method from a constructor 👦 You can make your constructor even after making this function! class A { public void A(){} public A(){} } This code will not show any error! And these will work perfectly!
23rd Aug 2020, 6:08 AM
Namit Jain
Namit Jain - avatar