Java Variables | Types of Variables In Java (Local Variable, Instance Variable, Static Variable)Java Variables are memory pieces those can contain values of data. In Java variables, every variable has its data type, which represents variable's memory size and layout; the operations those can be applicable to the Java variables. Java variables are commonly used for storing the information which Java program needs. The information can be of any type codes, texts, numbers, etc. In Java variables, every variable must be declared before it can be used. The declaration of Java Variables is as : data type variable1 [ = value][, variable2 [= value][, ...] ; There are mainly three types of variables in Java. All these types of variables can be differ by their declaration. The following are the different types of variables which we going to discuss.
Local variable can be declared in methods, blocks or constructors. This type of variables can be destroyed once it come out from the method, block or constructor. In the three types of variables, local variables can't have default values. So, local variables must be declared and assigned some values before using them. Instance variable can be declared with in a class and outside of method, block or constructor. A slot is created for each instance variable value, when a space for an object is allocated in the heap. Instance variable can be created when ever an object is created by using the keyword 'new' and it can destroy if the object is destroyed. Instance Variable can take access modifiers. Instance variables can directly accessed, by calling name of the variable with in the class. Static variable can also be called as Class variable. The static variable can be declared with the keyword static and this static variable can be declared with in a class and outside of method, block or constructor. In all three types of variables, static variable can occasionally used rather than declared as constants. Static memory is used for storing static variable. Static variable is rarely used apart from declaring final and use as either private or public constants. We can access the static variable by calling with the class name as ClassName.VariableName. Static variable can be created at the program begins and destroyed at the program ends. Apart from these, this chapter also include the topics like Type Conversion and Type Casting which explained deeply at below. In this Java Variables section, we'll go into detail about: 3.1 Java Variables
3.2 Scope Of Variables In Same Block
3.3 Type Conversion In Java
3.4 Type Casting In Java
Dependent Topics : 2. Data Types |