Declaring Variables in Java

Hello Everybody, Since we discussed about how to declare Classes,Methods in my previous posts. Now we discussed about  Varibles..Variables are  also class members defined within a class, which hold values. we had already discussed about the rules for naming a legal variable.

Here we focus on declaring primitive,reference and local variables.Apart from this the variables declared using certain keywords, such as final,transient and volatile.

  • Primitive variables
  • Reference variables
  • Local variables
  • Instance variables
  • Final Variables
  • Transient Variables
  • Volatile Variables

Primitive Variables

The variables of primitive type are known as primitive variables. For ex: if we need a variable to hold an integer value in a program, we need to declare an int type variable. The type of a primitive variable can never be changed after declaration, instead, the value can be changed throughout a program.

Suppose we have declared an int type variable i the type of the i  variable  can never be changed,but the value of i can be changed as and when required.

A primitive variable can be of following eight types:

  1. char
  2. boolean
  3. byte
  4. short
  5. int
  6. long
  7. double
  8. float

The primitive variables can be declared as class variables, instance variables, method parameters, or local variables. More than one primitive variable can be declared in a single line. see the bellow code.

  1. //declaring an int primitive variable
  2. int num;
  3. //declaring a boolean primitive variable
  4. boolean myBoleanValue;
  5. //declaring three int primitive variables in a single line
  6. int a,b,c;

Here we also focus on the sequence form for an integer type variable:(small to big) byte,short, int and long. we see one example for declaring an int type primitive variable,intializes and print the value.

  1. package com.vbbs;
  2. class MyClass
  3. {
  4.   // declaring an int primitive variable
  5. num=10;
  6. System.out.println(“The value of primitive int variable is:” + num);
  7. }
  8. public static void main(String[] args)
  9. {
  10. MyClass obj = new MyClass();
  11. obj.display();
  12. }
  13. }

Reference Variables

The variables referencing an object stored in the computer memory are known as reference variables.

The reference variables can be declared as static, local, or instance. One or more reference variables can be declared, as shown the following code:

  1. //declaring reference variables
  2. Book myBook;
  3. //declaring three String reference varaibles
  4. String str1, str2, str3;

Instance variables

Instance variables declared inside a class. When the class is instantiated, the instance variables are initialized. The instance variables also known as fields, property, or attributes of the class.

For example, in the below code, the bookName, bookId and bookTitle instance variables are defined for the Book class:

  1. package com.vbbs;
  2. Class Book
  3. {
  4.    //declaring the instance variables
  5. private String bookName;
  6. private String bookId;
  7. private String bookTitle;
  8. // provide code for accessing private instance variables
  9. }

Here the Book class declares the instance variables that can be accessed through each instance of the Book class. Means each instance of the Book class will have its own unique values for the three variables.

Some important rules for instance variables are:

  • we can use public,private, protected, or default access modifier.
  • We can be marked as final or transient.
  • We cannot be marked abstract, synchronized, static or native.

Local Variables

The variables that are declared within a method  are known as local variables. as the local variables are defined and initialized within a method, so their access is restricted to within  the method. They cannot be declared as public, private, protected, transient, abstract, volatile or static. They can only be marked as final.

  1. package com.vbbs;
  2. ………
  3. class Mylocal {
  4.     //declaring a method for declaring, initializing and using a local variable
  5. public void display()
  6. {
  7. int num=10;
  8. System.out.println(num);
  9. }
  10. …..
  11. }

In the above code, the local variable num is declared, initialized,  and accessed within the display() method.

A local variable cannot be accessed outside the method in which it is declared. suppose if a programmer wishes to access the num variable in any other method then a compilation error occurs. It is also possible to declare a local variable with the same name as the instance variable, as shown in the bellow:

  1. package com.vbbs;
  2. class MyLocal {
  3.    //declaring an instance variable
  4.    int num=15;
  5.    //declaring a method for declaring, initializing  and using a local variable
  6. public void display()
  7. {
  8.   // declaring a local variable
  9.      int num=10;
  10.  System.out.println(“the value of local variable is ” + num);
  11. }
  12.  public void disp()
  13. {
  14.  // accessing an instance variable
  15.  System.out.println(“The value of instance variable is”+num);
  16. }
  17. public static void main(String[] args) {
  18. MyLocal local = new MyLocal();
  19. local.display();
  20. loca.disp();
  21. }
  22. }

Here the MyLocal class declares an instance variable and a local variable of the same name num. it shows the output of the compilation code

The value of local variable is10                                                                                        

 The value of instance variable is15

As a result, the local and instance variables have the same name in a class. However, the argument name and the name of instance variable cannot be the same. For example see this:

  1. …..
  2. class MyLocal {
  3.    int num=10;
  4. public void setNum(int num) {
  5.     // compiler does not  compile as which num value needs to be equal to which num  value
  6.    num = num;
  7. }
  8. }

In the above code, to specify that the  num instance variable is assigned a value equal to that of the num parameter, the this keyword is used.

  1. …..
  2. class MyLocal {
  3.    int num=10;
  4. public void setNum(int num) {
  5.    this.num = num;
  6. }
  7. }

now it is clear that this.num specifies the current object’s instance variable.The num on the right-hand side implies the parameter.

Final variables

When a variable as declared as final, the value of that variable cannot be changed further. declaring a variable with the final keyword makes it impossible to re-initialize the variable after declaration. For example:

  1. …..
  2. class MyLocal {
  3.  final  int num=10;
  4. publicdisplay() {
  5.   // code to access the final variable num
  6. }
  7. }

In the above code, the final variable num is declared and it can be accessed by display() method, but the num variable cannot be re-initialized to a new value.

Transient Variables

The variables declared as transient are skipped by the JVM when you attempt to serialize the object containing it. means the variables declared as transient indicates that they are not part of the persistent state of an object.

  1. ……..
  2. class MyTrans
  3. {
  4.    int num1,num2;
  5. transient float rho, theta;
  6. }

If an instance of the MyTrans class is saved in the persistent storage, then only the num1 and num2 variables are saved. The transient variables are skipped during serialization.

Serialization is the process of saving the current state of an object to a stream and is used in the I/O stream.

Volatile Variables

Only the instance variable can be declared as volatile. in such cases, a thread reconciles its working copy of the variables with the master copy stored in the computer memory. This is not the important but  in mind can be used on that the volatile keyword can be used only while declaring the instance variables.

Static Variables

The static keyword is used with the declaration of class members to indicate that the class members exist independent of any instance created for the class. In other words, while creating an instance of a class, there will be a single copy  of the static members, irrespective of the number of instances created.

Already we discussed in previous post static keyword in Java…see that post click here.Good luck.

Leave a Reply

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