Instance Variables and Methods in java
When using object instance and class data type structures in Java programming, it is crucial to understand and apply the concepts of class object instance parameter variables and instance methods in class programming. So, let’s explore class object instances and class data types in detail in Java programming.

Instance Variables in Java Classes.
Instance variables in Java class programming are those variables, also known as class object fields or attributes. These are class parameter or data type variables that are declared or defined within a user-defined class data type but outside of a class declaration method or constructor method. Each class object created within a user-defined class has its own concept or method for copying or accessing these variables.
Elements of Instance Variables.
- Instance variables declared in a Java class program represent the current state or defined attributes of a class object.
- Instance variables declared in a Java program are not static in nature, meaning that each class object defines its own unique copy of the class data type.
- Instance variable parameters in a Java class program can be initialized or manipulated in a constructor method or at the location of the class declaration.
Example of declaring an instance variable in a Java class.
public class Employee {
// Here we create an employee instance variable attribute
String emp_name;
String age;
String address;
int salary;
// Here we create a constructor to initialize instance class variables
public Employee(String emp_name, String age, String address, int salary) {
this.emp_name = emp_name;
this.age = age;
this.address = address;
this.salary = salary;
}
// here we define Method to display information about the employee class
public void empInfo() {
System.out.println(“employee emp_name is – ” + emp_name);
System.out.println(“employee age is – ” + age);
System.out.println(“employee address is – ” + address);
System.out.println(“employee salary is – ” + salary);
}
public static void main(String[] args) {
// here we are Creating an object of Employee class
Employee testEmployee = new Employee(“Bhavishi Deora”, “21”, “Delhi”, 9999);
// here we Accessing instance variables via the class object
System.out.println(“Employee emp_name – ” + testEmployee.emp_name); //here it Accessing emp_name variable
System.out.println(“Employee age – ” + testEmployee.age); //here it Accessing age variable
System.out.println(“Employee address – ” + testEmployee.address); //here it Accessing address variable
System.out.println(“Employee salary – ” + testEmployee.salary); //here it Accessing salary variable
// here it Calling a method that uses the instance variables
testEmployee.empInfo(); // here it Calling method on testEmployee object
}
}
Explanation of Java class instance variables.
- Here, emp_name, age, address, and salary are defined as instance variables in the Employee class instance.
- These are unique to each class object in the Employee class. Different Employee class objects can have different values defined for emp_name, age, address, and salary.
Java Class Instance Methods.
Instance methods in a Java program are class methods that deal with instance variable attributes or parameters of an object in a user-defined class. Instance methods in Java can only be called or defined on a declared or defined instance object of the class.
Element of Java Class Instance Methods.
- Instance methods in a Java class can provide immediate access and management of class variables from the instance.
- Instance variable data types are not declared or defined as static data types in the class, meaning that a class object is required to invoke the instance variable.
- Instance variables can generally be used to define or declare the behaviour of a class object. For example, the class object can perform the actions or operations of the instance variable in the class program.
Example of a Java class instance method.
public class Employee {
// here we declare an Instance variables attributes for employee
String emp_name;
String age;
int salary;
// here we define Constructor to initialize instance variables
public Employee(String emp_name, String age, int salary) {
this.emp_name = emp_name;
this.age = age;
this.salary = salary;
}
//here class Instance method used to display employee class data information
public void empInfo() {
System.out.println(“Employee emp_name is – ” + emp_name);
System.out.println(“Employee age is – ” + age);
System.out.println(“employee salary is – ” + salary);
}
// here Instance method used to update the employee salary data
public void updateSalary(int newsalary) {
this.salary = newssalary; // here we Changing the instance variable previous value
}
public static void main(String[] args) {
// here we Creating an object of the Employee class data
Employee testEmployee = new Employee(“Siddhi Deora”, “19”, 11000);
// here it Calling class instance method to display employee info
testEmployee.empInfo(); // here it Display initial class details
// here it is Calling class instance method to update the pervious salary
testEmployee.updateSalary(15000); // here it Updating the salary of the employee
// here it Calling class instance method again to display updated employee info
testEmployee.empInfo(); // here it displays updated employee data info
}
}
Explanation of Java class instance methods.
- In this Employee class, empInfo() and updateSalary() are instance methods defined within the Employee class. They are not defined as static data types, and are dealing with user-defined class object instance variables.
- Whereas instance methods defined within a class can modify instance variables if needed, and can be used to perform desired operations on the class object.
Instance variables vs. local variables vs. class (static) variable data types in a Java class.
Instance variables in a Java class.
- Instance variables are defined within a Java class program, but are not declared outside of any user-declared method.
- Each object of a Java class has its own unique calling copy method.
- Instance variables represent the state of the object in the class program.
- Java class instance variables can be initialized in the constructor or at the location of declaration.
Local variables in a Java class.
- Local variables in a Java class are declared or defined within the class constructor or block.
- Local variables in a class are accessed and managed only within the method constructor or block where they are declared in the class program.
- Local variables in a Java class do not have a default value and must be initialized in the program before using them.
Static variables/class variables in a Java class.
- Static class data type variables are declared using the static keyword in a Java program. Static variables are shared or spread across all instances of a class, meaning there is only one copy of a static variable.
- Remember, static variables in a Java program are initialized or defined with a user-defined numeric value only once at the start of the program.
Example of the difference between instance variables and local variables and class (static) variables in a Java program.
public class Employee {
// here we create class Instance variable data type
String emp_name;
String age;
int salary;
// here we define Static variable used to share between all instances
static int totalOfEmployees = 0;
// here we defie Constructor to initialize instance variables data type
public Employee(String emp_name, String age, int salary) {
this.emp_name = emp_name;
this.age = age;
this.salary = salary;
totalOfEmployee++; //here it Increment the static variable number
}
// here we define Instance method to display employee class information
public void employeeInfo() {
System.out.println(“Employee emp_name is – ” + emp_name);
System.out.println(“Employee age is – ” + age);
System.out.println(“Employee salary is – ” + salary);
}
// here Static method to display the number of employee data information
public static void representNumberOfEmployee() {
System.out.println(“Total Number of employee is – ” + totalOfEmployee);
}
public static void main(String[] args) {
// here we Creating objects of Employee class data
Employee Employee1 = new Employee(“Bhavishi Deora”, “21”, 9999);
Employee Employee2 = new Employee(“Siddhi Deora”, “19”, 11000);
Employee Employee3 = new Employee(“Harry Deora”, “22”, 14000);
// Here we used calling the instance method to display employee class data information
Employee1.employeeInfo();
System.out.println(“\n”);
Employee2.employeeInfo();
System.out.println(“\n”);
Employee3.employeeInfo();
// Here we called the static method to display the employee number data
Employee.representNumberOfEmployee(); // Here we accessed the static method with the class name
}
}
Summary of instance variables and methods in a Java program.
Java instance variables concept.
- In Java programs, instance variables are defined specifically for each object instance of a class.
- Instance variables declared in a class program store and manage the current state of the object.
- Instance variables declared in a class program are accessed and managed through the class object.
Java instance methods concept.
- Variables declared in instances in a Java class program can be accessed and modified at any time.
- Instance methods are defined without the static keyword. Instances declared in a Java class program perform these operations on the data of a single object.
