Você está na página 1de 9

Variables in Java

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;

Different Primitive Data Types


Java supports the below mentioned primitive data types.
byte
short
int

long
float
double
char
boolean
Note*: Corresponding Wrapper Classes is also available.

Variable Types in Java


Variables in Java can be defined anywhere in the code (inside a class,
inside a method or as a method argument) and can have different
modifiers. Depending on these conditions variables in Java can be
divided into four categories.
Instance Variable

Static Variable
Local Variable
Method Parameter

Instance Variable(Non Static Fields)


Instance variables are used by objects to store their states. Variables
which are defined without the STATIC keyword and are outside any
method declaration are object specific and are known as Instance
Variables. Such variables are called as instance variables because their
values are instance specific and values of these variables arenot shared
among instances.
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 /*
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 }

Class Variable(Static Fields)

Variables which are declared with a STATIC keyword inside a class


(outside any method) are known as Class variable / Static variable. They
are known as Class level variable because values of these variables are
not specific to any instance but are common to all instances of a class.
Such variables will be shared by all instances of an object.
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 /*
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 }

Local Variables(Method Local)


When a variable is declared inside a method it is known as method local
variable. Scope of local variables is only inside the method, which means
local variables cannot be accessed outside that method. There are some
restrictions on access modifier that can be applied on Local variables. To
know more about access modifier CLICK HERE.
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 /*
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 }

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 }

Naming Rules Applied to Variables

Você também pode gostar