r/learnjava • • Jan 02 '26

Can someone please help me with Constructors?

I don't know why but I simply cannot wrap my brain around constructors. I know other languages and was able to pick them up rather easily, but for some reason Java is just so much more difficult for me, especially when it comes to constructors.

6 Upvotes

37 comments sorted by

View all comments

4

u/LookAtYourEyes Jan 02 '26

Can you explain your understanding what they are in other languages? And where the difference causes confusion for you? It's hard to know if you mean how to write them syntactically, or conceptually what they are doing that is confusing you.

1

u/Numerous_Training_20 Jan 02 '26

It's more that I'm struggling with the syntax behind it, but I'm finding both to be quite difficult. I've looked over so much sample code that my eyes are literally hurting. I've been able to bs some of it into working, but for the most part I'm just unable to figure it out.

5

u/LookAtYourEyes Jan 02 '26

Maybe let's start with what you consider a class to be, if you're familiar, and what you consider to be an object, if you're familiar. Do you know the difference?

2

u/vowelqueue Jan 03 '26

Syntax is pretty straightforward. You can think of a constructor as a special method that returns an instance of an object. You declare a constructor in a similar way to declaring a method, but the return type is omitted and the method name is the same as the class name. Like with methods you can overload constructors - declare multiple constructors will different parameters.

0

u/[deleted] Jan 02 '26

[deleted] — view deleted comment

0

u/behusbwj Jan 03 '26

They are what allow you to create an instance of a class. The second part is incorrect. The constructor doesn’t guarantee anything about what is stored or thrown away or used to instantiate a calculated field