Variables in Java

Variables in Java

A variable is something which is associated with a value can be changed. For example: int i=10; Here variable name “i” which is associated with with value 10, int is a data type that represents that this variable can hold integer values.

Note: We will cover the data types in the next tutorial. In this tutorial, we will discuss about variables.

How to Declare a variable in Java

The syntax to declare the variable is: data_type variable_name = value; here value is optional because in java, you can declare the variable first and then later assign the value to it. int num; Similarly we can assign the values to the variables while declaring them: char ch = ‘A’; int number = 100; or we can do it like this: char ch; int number; … ch = ‘A’; number = 100;

Variables naming convention in java

  • Variables naming cannot contain white spaces, for example: int roll number = 10; is invalid because the variable name has space in it.
  • Variable name can begin with special characters such as $ and _, for example int $number.
  • As per the java coding standards the variable name should begin with a lower case letter, for example int number; For lengthy variables names that has more than one words do it like this: int rollNumber; int serialNumber; (start the second word with capital letter).
  • Variable names are case sensitive in Java.

Types of Variables in Java

There are three types of variables in Java.

  • Local Variable
  • Static (or class) variable
  • Instance variable

1) Local Variable

Local variables are declared inside within the method of the class i.e their scope is limited to that method in which it is declared, you can’t change their values and access them outside of the method.

Example:

1
2
3
4
5
6
7
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">VariableExamples</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">myMethod</span><span class="hljs-params">()</span></span>{
        <span class="hljs-comment">//local variable</span>
        string myVar= <span class="hljs-string">"Inside Method..."</span>;
        System.out.println(myVar);
        }
}
Output:

Inside Method…

Note: If I hadn’t declared the instance variable and only declared the local variable inside method then the statement System.out.println(obj.myVar); would have thrown compilation error. As you cannot change and access local variables outside the method.

2) Static (or class) variable

Any variable when declared with the keyword “static” is known as static variable or class variable in JAVA. Static variable is used to fulfill the common properties of all objects. For example, If I create three objects of a class and access this static variable, it would be common for all, the changes made to the variable using one of the object would reflect when you access it through other objects.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StaticVarExample</span> </span>{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String myClassVar=<span class="hljs-string">"class or static variable"</span>;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{
        StaticVarExample obj = <span class="hljs-keyword">new</span> StaticVarExample();
        StaticVarExample obj2 = <span class="hljs-keyword">new</span> StaticVarExample();
        StaticVarExample obj3 = <span class="hljs-keyword">new</span> StaticVarExample();
       
        <span class="hljs-comment">//All three will display "class or static variable"</span>
        System.out.println(obj.myClassVar);
        System.out.println(obj2.myClassVar);
        System.out.println(obj3.myClassVar);

        <span class="hljs-comment">//changing the value of static variable using obj2</span>
        obj2.myClassVar = <span class="hljs-string">"Changed Text"</span>;

        <span class="hljs-comment">//All three will display "Changed Text"</span>
        System.out.println(obj.myClassVar);
        System.out.println(obj2.myClassVar);
        System.out.println(obj3.myClassVar);
    }
}
Output:

class or static variable class or static variable class or static variable Changed Text Changed Text Changed Text

3) Instance variable

Each instance(objects) of class has its own copy of instance variable. Unlike static variable, instance variables have their own separate copy of instance variable. We have changed the instance variable value using object obj2 in the following program and when we displayed the variable using all three objects, only the obj2 value got changed, others remain unchanged. This shows that they have their own copy of instance variable.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InstanceVarExample</span> </span>{
    String myInstanceVar=<span class="hljs-string">"instance variable"</span>;
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{
        InstanceVarExample obj = <span class="hljs-keyword">new</span> InstanceVarExample();
        InstanceVarExample obj2 = <span class="hljs-keyword">new</span> InstanceVarExample();
        InstanceVarExample obj3 = <span class="hljs-keyword">new</span> InstanceVarExample();

        System.out.println(obj.myInstanceVar);
        System.out.println(obj2.myInstanceVar);
        System.out.println(obj3.myInstanceVar);
       
        obj2.myInstanceVar = <span class="hljs-string">"Changed Text"</span>;

        System.out.println(obj.myInstanceVar);
        System.out.println(obj2.myInstanceVar);
        System.out.println(obj3.myInstanceVar);
    }
}
Output:

instance variable instance variable instance variable instance variable Changed Text

Related Post

Create UITableView (Table View) By the use of stor... How to use UITableView in Above IOS 8 and Swift (UITableView) In this tutorial we are study how to use the TableView in Swift. One of the most common...
Emailing Phrases Most useful emailing phrases There are some standard phrases that are used in business or formal letters. These are essential to give a professional ...
Invitation Letter for Visa Invitation Letter Samples These letter are necessary for taking visa of another country. so these samples are the most important document. Sample 1 ...
Drag and Drop Table View cell in IOS 11 & swi... Drag and Drop Table View cell in IOS 11 & swift 4
Loop Control Statement Loop control statements are used when a section of code may either be executed a fixed number of times. C gives you a choice of three types of loop st...
Dart Introduction | Dart programming language Dart Introduction Dart is a scalable language that you can use to write simple scripts or full featured apps. Whether you’re creating a mobile app, w...

Leave a Reply

Your email address will not be published. Required fields are marked *