In any programming language, the program needs identifiers for storing different values that can be used throughout the program. These identifiers are variables.
Bạn đang xem: Java instanceof keyword
Variable in JavaA variable is a name assigned lớn a value that is stored inside the system memory. The value can be updated during the program execution.In Java programming, the variables used for the program need khổng lồ declare them first.The variable is declared using a data type followed by the identifier name. The variable can be initialized at the time of declaration or it can be assigned a value taken from the user during the program execution.There are basically three types of variables in Java,Java Local variableJava Instance variableJava Static variable / Java class variable
Java Instance VariableThe variables that are declared inside the class but outside the scope of any method are called instance variables in Java.The instance variable is initialized at the time of the class loading or when an object of the class is created.An instance variable can be declared using different access modifiers available in Java lượt thích default, private, public, và protected.Instance variables of different types have mặc định values that are specified in the next point.
FeaturesTo use an instance variable an object of the class must be created.An instance variable is destroyed when the object it is associated with is destroyed.An instance variable does not compulsory need to lớn be initialized.Instance variables are accessible inside the same class that declares them.
Limitations of Instance VariableIt cannot be declared static, abstract, striftp, synchronized, and native.It can be declared final and transient.It can be of any of the four access modifiers available in Java (private, public, protected, và default).
Default Values of Instance Variables in Java
The instance variables in Java are of different data types as follows;
Let"s use instance variable in Java program.
Using Instance Variable in a Java Program
In the following Java program, a class Studentsrecords is declared và instance variables are created using different access modifiers.
public class Studentsrecords /* declaration of instance variables. */ public String name; //public instance String division; //default instance private int age; //private instance /* Constructor that initialize an instance variable. */ public Studentsrecords(String sname) name = sname; /* Method to intialize an instance variable. */ public void setDiv(String sdiv) division = sdiv; /* Method khổng lồ intialize an instance variable. */ public void setAge(int sage) age = sage; /* Method lớn display the values of instance variables. */ public void printstud() System.out.println("Student Name: " + name ); System.out.println("Student Division: " + division); System.out.println("Student Age: " + age); /* Driver Code */ public static void main(String args<>) Studentsrecords s = new Studentsrecords("Monica"); s.setAge(14); s.setDiv("B"); s.printstud();
The above Java program initializes the instance variables by declaring an object of the class Studentsrecords. The values of instance variables name, division, và age are displayed using printstud() method.
Difference between Local, Instance and Static variables in Java
|1.||Variables declared within a method are local variables.||An instance variable is declared inside a class but outside of any method or block.||Static variables are declared inside a class but outside of a method starting with a keyword static.|
|2.||The scope of the local variable is limited to lớn the method it is declared inside.||An instance variable is accessible throughout the class.||The static variable is accessible throughout the class.|
|3.||A local variable starts its lifetime when the method is invoked.||The object associated with the instance variable decides its lifetime.||The static variable has the same lifetime as the program.|
|4.||Local variable is accessible lớn all the objects of the class.||Instance variable has different copies for different objects.||Static variables only have one single copy of the entire class.|
|5.||Used to lớn store values that are required for a particular method.||Used to store values that are needed to be accessed by different methods of the class.|
Xem thêm: Bài Soạn Văn Lớp 9 Đấu Tranh Cho Một Thế Giới Hòa Bình Của Gg Mác
|Used for storing constants.|