News

How to Initialize Instance Variables of a Class in Java

java instance variable output

Instance Variables

In our previous discussion we have discussed what is instance variable or member variable. Instance variables are the variables which is declared under a class. Now we will see how to initialize those variables of a class within the same class or even from another class.

We can accomplish the same in three ways:

1. By Object Reference

EmployeeDemo1.java

class Employee{
    String employeeName;
    String address;
    int age;
    double salary;
    void showDetails(){
        System.out.println("Employee's Name: "+employeeName);
        System.out.println("Employee's Address: "+address);
        System.out.println("Employee's Age: "+age);
        System.out.println("Employee's Salary: "+salary);
    }
}

class EmployeeDemo1{
    public static void main(String args[]){
        Employee employee = new Employee();
        employee.employeeName = "John";
        employee.address = "Los Angles";
        employee.age = 25;
        employee.salary = 34503.92;
        employee.showDetails();
    }
}

2. By Method Within The Same Class

EmployeeDemo2.java

class Employee{
    String employeeName;
    String address;
    int age;
    double salary;
    void initialize(String empName,String addr,int ag,double sal){
        employeeName = empName;
        address = addr;
        age = ag;
        salary = sal;
    }
    void showDetails(){
        System.out.println("Employee's Name: "+employeeName);
        System.out.println("Employee's Address: "+address);
        System.out.println("Employee's Age: "+age);
        System.out.println("Employee's Salary: "+salary);
    }
}

class EmployeeDemo2{
    public static void main(String args[]){
        Employee employee = new Employee();
        String employeeName = "John";
        String address = "Los Angles";
        int age = 25;
        double salary = 34503.92;
        employee.initialize(employeeName,address,age,salary);
        employee.showDetails();
    }
}

3. By Constructor

EmployeeDemo3.java

class Employee{
    String employeeName;
    String address;
    int age;
    double salary;
    Employee(String empName,String addr,int ag,double sal){
        employeeName = empName;
        address = addr;
        age = ag;
        salary = sal;
    }
    void showDetails(){
        System.out.println("Employee's Name: "+employeeName);
        System.out.println("Employee's Address: "+address);
        System.out.println("Employee's Age: "+age);
        System.out.println("Employee's Salary: "+salary);
    }
}

class EmployeeDemo3{
    public static void main(String args[]){
        String employeeName = "John";
        String address = "Los Angles";
        int age = 25;
        double salary = 34503.92;
        Employee employee = new Employee(employeeName,address,age,salary);
        employee.showDetails();
    }
}

We will learn more about Java Constructors in our next tutorial.

Output

java instance variable output

Explanation of the Java Code & Outputs

In the first case we have created one object employee of Employee class. After that we have initialized instance variables using the same object.

In the second case we have written a method initialize () within Employee class. Having created object of Employee class we called that method to initialize the instance variables.

In the third case we have created one constructor which takes parameters to initialize the instance variables.

We will discuss about constructors in Java in our next discussion.

Checkout more useful tutorials and definitive guidelines on Java programming here.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top