what does the constructor return?
hey guys. can smb explain this to me? while reading the book, i found this:
the implicit return type of a class’ constructor is the class type itself.
.
what does that even mean?
so if i do new Person()
what does the constructor return?21 Replies
⌛
This post has been reserved for your question.
Hey @bambyzas! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
it means you can use the
Person
constructor to create an object of Person
technically the sentence is a bit inaccurate but that doesn't matter that muchok. but as i see, constructors dont return a thing, right? bc they dont have a return type
or maybe something is going on under the hood?
Java makes sure you get an object of the class when using
new Person("John Doe", 25)
but
new
doesnt actually create an object. it just allocates memory
or no?and the constructor defines what happens when that object is created
the bytecode instruction, yes
so i can have this piece of code and it will be valid?
Person("asd",123);
?but when using
new Person(...)
, it creates the object and calls the constructor
no
In source code, you can only do these things togetherwhy? its just a method call
ohh
you cannot call the method without
new
it's a special methodTo create an object it's always
new
(somewhere down the line at least), and new
always creates an objectno 0x150, simplifications are necessary
internally, when your code is compiled, new does two things:
1. allocate memory for the new object (NEW instruction)
2. initialize that memory by calling the constructor method
the constructor method actually returns void, because you dont actually get a new instance out of it directly. it just initializes a newly allocated instance.
you dont need to worry about things like this, especially not if you’re a newcomer. the new keyword always just creates an instance with the constructor matching the arguments you provided, and it always calls a constructor.
and bytecode isn't really important
and no, you cant just do one thing or the other directly, the new keyword does both, always.
you can bypass calling the constructor and get an entirely empty instance of any type by using Unsafe, but i HIGHLY recommend staying away from that thing if you dont know exactly what it does and why you need it
Look at the level of questions we're asking here
This is completely unnecessary information for this person at this point
well he asked what the return type of a constructor is, the return type of a constructor method is void
simple as that
yes because
void item = new Thing();
compiles...and thats why i explained what new does
a constructor doesnt return anything, and the constructor method declaration when compiled is always void
i dont believe in lying to beginners, a constructor has a void return type and that's it
Yes, I know that, you know that, but that's an implementation detail at best
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.