Java Constructors Return Nothing, new Keyword Does

🚨 Java fact about constructors 👇 Constructors don’t have a return type… But something still returns an object 🤯 Example: class User { User() { System.out.println("Constructor called"); } } User user = new User(); 👉 What’s actually happening? - "new" keyword creates the object - Allocates memory - Calls the constructor - Returns the reference 👉 Important: Constructor itself does NOT return anything 👉 "new" keyword returns the object --- 👉 This is NOT a constructor: class User { void User() { } ❌ } 👉 It becomes a normal method 💡 Many people think constructor returns object… but actually new keyword does that Did you know this? 👇 #Java #CoreJava #Programming #BackendDeveloper #Coding #TechLearning

See more comments

To view or add a comment, sign in

Explore content categories