How to Create an Employee Class with Three Instance Variables in Java - Comprehensive Guide

As a developer, you know the importance of class and method declarations in writing program in Java. Classes are special types of objects, they are made up of methods, instance variables, and constructors all of which are used to create and manipulate an instance of itself. In this documentation post, we will look into how to create an employee class with three instance variables in Java, discuss the structure of an employee class and of each instance variable, give code examples and how to access class and its instance variables. Finally, we will have a questions and answers section to further discuss topics related to the content covered in this doc.

Structure of an Employee Class

The main purpose of a class is to create an object that has both data and methods that manipulate the data. To create an employee class, first you need to create a class named "Employee" and then add the instance variables and methods associated to it.

Each employee object in the class always contains three instance variables: name, role, and salary. Name is a String type variable that represents the employee’s name; role is also a String type variable that holds the employee’s job title or role; and salary is a Double type variable that holds the employee’s salary.

Code Examples

To create an employee class with the three instance variables, you can use the following code:

    public class Employee { 
  
        // Instance Variables 
        String name; 
        String role; 
        double salary; 
  
        // Constructor 
        public Employee(String name, String role,  
                        double salary) 
        { 
            this.name = name; 
            this.role = role; 
            this.salary = salary; 
        } 
  
        // Methods 
            // getters for Instance Variables 
        public String getName() { return name; } 
        public String getRole() { return role; } 
        public double getSalary() { return salary; } 
  
            // setters for Instance Variables 
        public void setRole(String role) { this.role = role; } 
        public void setSalary(double salary) { 
                                       this.salary = salary; } 
    } 

Accessing the Class and its Instance Variables

To create an instance of this class and its associated three instance variables, you can use the following code below:

    // create an instance of Employee 
    Employee emp1 = new Employee("Zahra azizi",  
                                "Software Engineer", 
                                8500.50); 
  
    // set variables of emp1 
    emp1.setRole("AI Engineer"); 
    emp1.setSalary(9500.50); 
  
    // accessing the instance variables 
    System.out.println("Name of the Employee: "
                       + emp1.getName()); 
    System.out.println("Role of the Employee: "
                       + emp1.getRole()); 
    System.out.println("Salary of the Employee: "
                       + emp1.getSalary()); 

FAQs

What is an employee class?

An employee class is a class of objects that contain properties such as name, role, and salary of the employee. Instance variables of the class are name, role, and salary.

How will you access the employee class and its instance variables?

To access the employee class and its instance variables, you can create an instance of the employee class, modify the instance variables and then access the instance variables as below:

 // accessing the instance variables 
 System.out.println("Name of the Employee: " 
                    + emp1.getName()); 
 System.out.println("Role of the Employee: " 
                    + emp1.getRole()); 
 System.out.println("Salary of the Employee: " 
                    + emp1.getSalary()); 

What is the importance of accessor and mutator methods?

Accessor and mutator methods are important in order to ensure the integrity of the data in a class. Accessors are also known as getters, and are used to return the value of an instance variable. For example, the getName() method returns the name of the employee. Mutator methods are used to change or update the value of an instance variable. For example, the setRole() method changes the role of the employee.

What is a constructor and how does it work?

Constructors are special methods that have the same name as the class and are used to initialize objects. The constructor method is used to initialize the instance variables of an object when it is created. It is invoked while creating an instance of the class, like in the code example above.

What is a String type variable?

A String type variable is a variable whose value is a sequence of characters. In Java, string variables are declared using the String keyword and are used to store words and sentences.

Sources

  1. Data Structures and Algorithms in Java - by University of California, San Diego
  2. Constructors in Java - by GeeksforGeeks

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Lxadm.com.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.