I am new to Java and I understand constructors(I guess), but why does my code does not work until I construct an empty constructor? When I already have a constructor with parameters etc of that class.
Asked
Active
Viewed 402 times
1

Md Golam Rahman Tushar
- 2,175
- 1
- 14
- 29

mrAKU
- 11
- 1
-
1Does this answer your question? [Java default constructor](https://stackoverflow.com/questions/4488716/java-default-constructor) – Šimon Kocúrek Mar 25 '20 at 20:02
-
it seems that you declared a constructor taking a number of arguments. so the no-arguments constructor is no longer available. You need to either declare a no-arg constructor, or to pass in the required arguments – Daniele Mar 25 '20 at 20:07
1 Answers
1
Because you don't have a constructor with no argument at your class. That's why when you are trying to create an instance using the no argument constructor it showing you the error.
Note that, If you don't define any constructor then the class have a default constructor with no parameter. But if you do declare a constructor then there is no default constructor, only the ones you define.
Hope this will clear your confusion.

Mark Rotteveel
- 100,966
- 191
- 140
- 197

Md Golam Rahman Tushar
- 2,175
- 1
- 14
- 29
-
I checked out a code from a tutorial which only had one constructor which was expecting few arguments. Everywhere in the code only that constructor was used. Even then I was getting error that can't access this class. So, when I added a no args constructor all errors went away. And now if I remove that no arg constructor, errors don't come now. Not sure, what caused those errors in the first place. – Akki Aug 20 '22 at 16:04