Can constructors be called as normal methods? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can constructors be called as normal methods?

30th Oct 2017, 10:21 AM
Morgan Lee
Morgan Lee - avatar
6 Answers
+ 14
@Morgan No, you can't. You should use setters if you want to do that.
30th Oct 2017, 12:01 PM
qwerty
qwerty - avatar
+ 13
Please define 'normal'... What do you mean? You can call a constructor without storing the returned object if this is what you want to know.
30th Oct 2017, 10:27 AM
Tashi N
Tashi N - avatar
+ 12
Shouldn't it be this.someRandomVariableToBeIncremented += number; ?
30th Oct 2017, 12:04 PM
qwerty
qwerty - avatar
+ 1
constructors are meant to initialise an object, but can it be called multiple times instead of recreating another method that does the exact same thing
30th Oct 2017, 10:31 AM
Morgan Lee
Morgan Lee - avatar
+ 1
Say, the constructor initialises the object with incrementing a variable passed on to the parenthesis //main Foo bar = new Foo(8); //constructor for class Foo Foo(int number){ this.someRandomVariableToBeIncremented += number; } after initialising this object, i want to add another 5 to the "someRandomVariableToBeIncremented" at that instance so can i call the constructor again using bar.Foo()? weird question, kinda dumb but i dont really get this part
30th Oct 2017, 10:56 AM
Morgan Lee
Morgan Lee - avatar
+ 1
@110000 yeah sorry, realised that and wanted to change it just now. Its fixed now, thanks for the help everyone ;_______;
30th Oct 2017, 12:37 PM
Morgan Lee
Morgan Lee - avatar