Menu
Topics Index
...
`

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.


Java Variables, Java Variables | Types of Variables In Java

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.
(a) Local variable
(b) Instance variable
(c) Static variable

Types Of Variables, Java Variables | Types of Variables In Java

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.

Local Variable, Java Variables | Types of Variables In Java

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.

Instance Variable, Java Variables | Types of Variables In Java

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.

Static Variable, Java Variables | Types of Variables In Java

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
  • Java Variables can be used to store information/data. Once a variable is declared, it can be initialized with some value and later can be changed to some other value. A variable can ...Read More

3.2 Scope Of Variables In Same Block
  • Scope of variable is nothing but the statements or expressions in which the variable can be used. Any variable is in scope ( valid ) in the statements below its ...Read More

3.3 Type Conversion In Java
  • Sometimes it is necessary to convert variables of one type to another type. e.g., convert values present in an int to byte or from short to long. Type conversion happens between ...Read More

3.4 Type Casting In Java
  • Type conversion is automatically done if the types are compatible and source type is smaller than destination type. But when destination type is smaller than source type, then ...Read More

Dependent Topics : 2. Data Types 

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App