Escolar Documentos
Profissional Documentos
Cultura Documentos
Introduction
In Java, objects store their states in variables. Variables are used as
containers to hold values (int, long, string) during the life cycle of an
application.
Variable Definition
To define a variable, we need to assign a data type for that variable. Data type
defines the kind of value this variable can hold (int, long or String etc.).
Example of variable definition
1 public final int var ;
public
Access Modifier applied to variable
final
Non Access Modifier applied to this variable
int
Data type. It defines kind of value this variable can hold (int in this case)
var
Name of the variable
Variable Initialization
Now that we are done defining a variable, we can initialize the above
variable by assigning a value to it . In this case, we assign the variable an
integer value.
1 public final int var = 9;
long
float
double
char
boolean
Note*: Corresponding Wrapper Classes is also available.
Static Variable
Local Variable
Method Parameter
8 /*
9 * Below variable is INSTANCE VARIABLE as it is outside any method and it is
10 * not using STATIC modifier with it. It is using default access modifier.
11 * To know more about ACCESS MODIFIER visit appropriate section
12 */
13 int instanceField;
14
15 /*
16 * Below variable is STATIC variable as it is outside any method and it is
17 * using STATIC modifier with it. It is using default access modifier. To
18 * know more about ACCESS MODIFIER visit appropriate section
19 */
20 static String staticField;
21
22 public void method() {
23 /*
24 * Below variable is LOCAL VARIABLE as it is defined inside method in
25 * class. Only modifier that can be applied on local variable is FINAL.
26 * To know more about access and non access modifier visit appropriate
27 * section.
28 *
29 * Note* : Local variable needs to initialize before they can be used.
30 * Which is not true for Static or Instance variable.
31 */
32 final String localVariable = "Initial Value";
33 System.out.println(localVariable);
34 }
35
36 public static void main(String args[]) {
37 VariablesInJava obj = new VariablesInJava();
38
39 /*
40 * Instance variable can only be accessed by Object of the class only as below.
41 */
42 System.out.println(obj.instanceField);
43
44 /*
45 * Static field can be accessed in two way.
46 * 1- Via Object of the class
47 * 2- Via CLASS name
48 */
49 System.out.println(obj.staticField);
50 System.out.println(VariablesInJava.staticField);
51 }
52 }
43
44 /*
45 * Static field can be accessed in two way.
46 * 1- Via Object of the class
47 * 2- Via CLASS name
48 */
49 System.out.println(obj.staticField);
50 System.out.println(VariablesInJava.staticField);
51 }
52 }
Parameters
Parameters are variables that are passed in methods. For example,
String args[] variable in main method is a parameter.
1 package com.jbt;
2
3 /*
4 * Here we will discuss about different type of Variables available in Java
5 */
6 public class VariablesInJava {
7
8 public static void main(String args[]) {
9 System.out.println("Hello");
10 }
11 }