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?
public class Person {
private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Person {
private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
so if i do new Person() what does the constructor return?
21 Replies
JavaBot
JavaBot14mo ago
This post has been reserved for your question.
Hey @bambyzas! Please use /close or the Close 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st14mo ago
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 much
i hate SQL so much its unreal
ok. 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?
dan1st
dan1st14mo ago
Java makes sure you get an object of the class when using new Person("John Doe", 25)
i hate SQL so much its unreal
but new doesnt actually create an object. it just allocates memory or no?
dan1st
dan1st14mo ago
and the constructor defines what happens when that object is created the bytecode instruction, yes
i hate SQL so much its unreal
so i can have this piece of code and it will be valid? Person("asd",123);?
dan1st
dan1st14mo ago
but when using new Person(...), it creates the object and calls the constructor no In source code, you can only do these things together
i hate SQL so much its unreal
why? its just a method call ohh
dan1st
dan1st14mo ago
you cannot call the method without new it's a special method
tjoener
tjoener14mo ago
To create an object it's always new (somewhere down the line at least), and new always creates an object
dan1st
dan1st14mo ago
no 0x150, simplifications are necessary
0x150
0x15014mo ago
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.
dan1st
dan1st14mo ago
and bytecode isn't really important
0x150
0x15014mo ago
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
tjoener
tjoener14mo ago
Look at the level of questions we're asking here This is completely unnecessary information for this person at this point
0x150
0x15014mo ago
well he asked what the return type of a constructor is, the return type of a constructor method is void simple as that
tjoener
tjoener14mo ago
yes because void item = new Thing(); compiles...
0x150
0x15014mo ago
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
tjoener
tjoener14mo ago
Yes, I know that, you know that, but that's an implementation detail at best
JavaBot
JavaBot14mo ago
💤 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.

Did you find this page helpful?