Instances in the Java program are created whenever an object is called in a class or function, which uses a variable for the purpose of allocating a memory storage unit for every time an object is called, and an instance is created. Any changes or operations performed on the instances will reflect automatically in the instance variable and the corresponding memory unit. This type of variables are supported by several data types, namely int, char, long, short, boolean, float, double, byte and object.
Syntax:
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
<datatype> <variable_name>; <datatype> <variable_name> = <initializing_value>;
There are mainly 3 types of variables:
Instance variables are declared/defined in a class but outside the constructor (s), method(s) and block(s). Instance variables are called so because they’re instance(object) specific and are not shared among instances (objects)s, and changes done to variables of a particular instance will not reflect on others. When memory is allocated for an object in a heap, a slot for each of the instance variables is created. Instance variables of an object are created when the object is created with ‘new’ and are destroyed when the object is destroyed. Objects use them to preserve the states.
Syntax:
public class Employee { public String Name; // instance variable with public Access. private int salary ; // instance variable with private access. public static String company; // not an instance variable as it is Static and the value it holds is // not instance but class specific. }
Instance variables have default values; therefore, they can be declared without initialization, unlike local variables. The default values for the variables depend on their data types.
|
Default Value | ||||||||||||||||||||
boolean | false | ||||||||||||||||||||
byte | 0 | ||||||||||||||||||||
short | 0 | ||||||||||||||||||||
int | 0 | ||||||||||||||||||||
long | 0L | ||||||||||||||||||||
char | u0000 | ||||||||||||||||||||
float | 0.0f | ||||||||||||||||||||
double | 0.0d | ||||||||||||||||||||
Object | null |
Objectreference.variable_name
Examples of the instance variable in java are given below:
Code:
import java.io.*; public class Employee { // this instance variable can be accessed any child class since it is public. public String Name; // Since salary is a private variable, it is visible in this class only. private int salary; // static variable is same among the instances public static String company; // The name variable is initialized in the constructor public Employee (String eName) { Name = eName; } public void setSalary(int eSal) { salary = eSal; } public void printEmp() { System.out.println("name : " + Name ); System.out.println("salary :" + salary); System.out.println("Company :" + Employee.company); } public static void main(String args[]) { Employee.company = "Google"; Employee employee_one = new Employee("Jack"); employee_one .setSalary(100000); employee_one .printEmp(); Employee employee_two = new Employee("Jill"); employee_two .setSalary(200000); employee_two .printEmp(); } }
Output:
In the above example, we can see the difference between instance variables and static variables. We have created two objects for employees. The name and salary of the objects are different, but the company is the same for both. This is because it is a static variable and the rest are instance variables.
By changing the line “private int salary; “ in the above code to “ private final int salary = 500;”. We’re changing the behavior of the variable. Since we’re trying to modify a final variable, the compiler throws the following error.
Output:
Code:
import java.io.*; public class DefaultValues { boolean val1; double val2; float val3; int val4; long val5; String val6; public static void main(String[] args) { System.out.println("Default values are :"); DefaultValues d = new DefaultValues(); System.out.println("Val1 = " + d.val1); System.out.println("Val2 = " + d.val2); System.out.println("Val3 = " + d.val3); System.out.println("Val4 = " + d.val4); System.out.println("Val5 = " + d.val5); System.out.println("Val6 = " + d.val6); } }
The above code snippet on execution results in the below output. As mentioned earlier, instance variables have default values if they are not initialized while declaring. This program prints the default values of all the data types.
Output:
Code:
import java.io.*; public class Test implements Serializable { // Normal variables int val1 = 10; int val2 = 20; // Transient variables transient int val3 = 30; // Use of transient has no impact here transient static int val4 = 40; transient final int val5 = 50; public static void main(String[] args) throws Exception { Test input = new Test(); // serialization FileOutputStream fos = new FileOutputStream("abc.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(input); // de-serialization FileInputStream fis = new FileInputStream("abc.txt"); ObjectInputStream ois = new ObjectInputStream(fis); Test output = (Test)ois.readObject(); System.out.println("val1 = " + output.val1); System.out.println("val2 = " + output.val2); System.out.println("val3 = " + output.val3); System.out.println("val4 = " + output.val4); System.out.println("va15 = " + output.val5); } }
Executing the above program results in the below-given output. variable val3 is declared transient, so its value was not stored into the byte stream when the Test object was serialized.whereas val4 and val5 are not instance variables because they violate the rules of instance variables. Hence their states remain unaffected.
Output:
The above is the detailed content of Instance Variable in Java. For more information, please follow other related articles on the PHP Chinese website!