Creating Objects in java

Creating Objects in java

In Java object-oriented programming, objects are instances of a class, or elements of a class structure or variable created from an object. When Java users create a new class object in a class program, they are creating a new instance of a real class object. This class object contains user-defined custom data types, data fields, and class Behavior methods.

Creating Objects in java

Steps to create a new object in Java programming.

  • Define a class – This serves as a foundational structural concept for creating new class objects in Java programs.
  • Use the new keyword – The new keyword is used in Java class programs to create a new class object instance.
  • Call a constructor – When Java users create a class object in an existing class, it is used to call the class constructor created in the class to initialize the created class object.

Syntax for creating a new object in a Java class.

ClassName objectName = new ClassName();

Example of creating a new class object in Java programming.

Here we create a new class program named Employee, which represents an employee data set with Employee class attributes such as emp_name, age, and salary. Next, we will create a new object for the Employee class.

public class Employee {

// Here we set the employee class fields with its data type

String emp_name;

String age;

int salary;

String address;

// Employee class Constructor, initialize the class object

public Employee(String emp_name, String age, String address, int salary) {

this.emp_name = emp_name; // Here we assign the emp_name of the employee class

this.age = age; // here we Assign the age of the employee class

this.address = address; // here we Assign the address of the employee class

this.salary = salary; // here we Assign the salary of the employee class

}

// Method to display employee class element

public void empInfo() {

System.out.println(“\n Employee emp_name is – ” + emp_name);

System.out.println(“\n Employee age is – ” + age);

System.out.println(“\n Employee address is – ” + address);

System.out.println(“\n Employee join Year is – ” + salary);

}

// here we create a Main method to create employee class objects

public static void main(String[] args) {

// Creating an object of the Employee class

Employee myEmployee = new Employee(“Bhavishi Deora”, “21”, “New Delhi”, 2022);

// Creating another object of the Employee class

Employee newEmployee = new Employee(“Siddhi Deora”, “19”, “Mumbai”, 2024);

// Calling the method to display details of each employee

myEmployee.empInfo();

newEmployee.empInfo();

}

}

Explanation of creating a new class object.

Class definition (employee).

  • Employee class has four fields – Here in Employee class, emp_name, age, salary, and address are class fields. Which represent the attributes of the Employee class.
  • When a new Employee object is created in the Employee class, the Employee constructor initializes the Employee(String emp_name, String age, String address, int salary) fields.
  • Here, the method empInfo() prints the details information of the Employee class.

Creating new objects in the Employee class.

Employee myEmployee = new Employee(“Bhavishi Deora”, “21”, “New Delhi”, 2022) creates a new object of type Employee, myEmployee, and initializes it with the class object values ​​”Bhavishi Deora”, “21”, “New Delhi”, 2022 for emp_name, age, address, and salary, respectively.

Employee newEmployee = new Employee(“Siddhi Deora”, “19”, “Mumbai”, 2024); creates another object, the newEmployee class object, with different values.

Calling methods on the Employee class object.

myEmployee.empInfo(); calls the empInfo method on the myEmployee object and prints its details to the console screen.

newEmployee.empInfo(); performs the same process for the newEmployee object.

Things to remember when creating a new object in a Java class.

Creating a Java Class Object.

  • To create an object of a user-defined class in a Java class, use the class constructor method followed by the new keyword.
  • The new constructor in a Java program is responsible for initializing the object’s attributes.

Accessing new fields and methods in a class object.

  • In a Java program, users can access and manage the fields and methods of a class object by using the . operator. For example, myEmployee.empInfo() or newEmployee.empInfo() can access class fields.

Creating Multiple Objects in a Java Class.

Java programmers can create as many new class objects as they want from a single class. Each created class object has a copy of its class fields and calling methods.

Java Class Constructor Overloading Method.

Java programmers can create or define multiple user-defined constructors in a class with multiple variable parameters. This process is known as constructor overloading.

Example of Java class constructor overloading.

public class Employee {

// Here we set the employee class fields with its data type

String emp_name;

String age;

int salary;

// Create a constructor with variable parameters

public Employee(String emp_name, String age, int salary) {

this.emp_name = emp_name;

this.age = age;

this.salary = salary;

}

// create Constructor with default user define values

public Employee() {

this.emp_name = “Unknown”;

this.age = “not defined”;

this.salary = 000;

}

// Method to display employee class element

public void empInfo() {

System.out.println(“Employee Name is – ” + emp_name);

System.out.println(“Employee Age is – ” + age);

System.out.println(“Employee Salary is – ” + salary);

}

public static void main(String[] args) {

// Creating an object of the Employee class

Employee myEmployee = new Employee(“Bhavishi Deora”, “21”, 2022);

// here we’re creating an object using the default constructor

Employee defaultemployee = new Employee();

myEmployee.empInfo(); // here it displays myEmployee details

defaultemployee.empInfo(); // here it displays defaultemployee details

}

}

Java class constructor overloading explanation.

  • In this example, you have two class constructors defined: one that initializes the class fields with a specific Employee class value, and another that assigns and displays a default class element value.

Leave a Reply