Java Constructor Basics: Initialization and Types

#60DaysOfJava 📚 Day 16 Constructor in Java 🔹 Constructor in Java 👉 Constructor is a special method 👉 Using the constructor we initialize objects at object creation time. 👉 Constructor is called at instance creation time 👉 While constructor is invoked, memory for the object will be allocated. 👉 Constructor doesn’t return anything and we don’t need to mention return type. 👉 abstract, static, final, and synchronized cannot be used with constructor 👉 We can use access modifiers (private, protected, public, default) to control object creation. 🔹 How to Create Constructor Syntax: AccessModifier ClassName() { } 👉 Example: public Employee(String name, double salary) { } 👉 Constructor will be called at object creation time 🔹 Types of Constructor 👉 No Parameterized Constructor 👉 Parameterized Constructor 👉 Copy Constructor 🔹 Example class Employee { String name; double salary; double bonous; 🔹Non parameterized constructor public Employee(){ System.out.println("Constructor called"); } 🔹Parameterized constructor public Employee(String empName,double empSalary){ name = empName; salary = empSalary; } 🔹 Parameterized constructor with different parameter size public Employee(String empName,double empSalary,double empBonous){ name = empName; salary = empSalary; bonous = empBonous; } 🔹 Copy constructor public Employee(Employee employee){ name = employee.name; salary = employee.salary; bonous = employee.bonous; } } 🔹 Notes 👉 Non-parameterized constructor is not mandatory compiler will add it by default 👉 Inside default constructor, values are not initialized explicitly 👉 JVM assigns default values based on data types 👉 Parameterized constructor ➡️ We can overload like normal methods (different type/size) 👉 Copy constructor ➡️ Used to copy values from one object to another ➡️ Not available by default in Java we need to define it 🤵 Follow Hariprasath V for daily more helpful resources. ♻ Repost Others also learn and grow together 👍 Hit if this was helpful. ✅ Save it future use. ================================================ #60DaysOfJavaWithHariprasathv6 #Java #JavaBasics #Programming #Coding #Developers #LearningJava #HighLevelDesign #SystemDesign #DSAChallenge #60DaysOfDSA #ProblemSolving #CodingJourney #Consistency #LearnByDoing #DataStructures #Algorithms #InterviewPrep #KeepCoding #Productivity #Focus #DreamBig #Java #SystemDesign #DataStructures #Algorithms #JavaDeveloper #DSA #CodingInterview #TechInterview #SystemDesignInterview #BackendDevelopment #SoftwareEngineering #JavaInterview #LeetCode #InterviewPrep #DataStructureAndAlgorithms #DesignPatterns #LowLevelDesign #Multithreading #SOLIDPrinciples #RESTAPI #BackendEngineer #CodeInterview #interviewtips #interviewexperience #Java #Programming #CoreJava #Learning #Developers #OOP

  • graphical user interface

public Employee(String empName,double empSalary){ name = empName; salary = empSalary; } Instead giving empName we will use name as parameter right but we will face issue i will exlain what is issue and how to solve on further post Follow Hariprasath V

To view or add a comment, sign in

Explore content categories