Variables in Java

By | July 26, 2020

Variables in Java are a bucket or container that used to contains the value or technically it is defined as the identifier used for giving the name to memory.

Types of Variables in Java

There are two types of variables are present in Java.

  1. Local Variable. 
  2. Global Variable.

Local Variable

It is a variable that is declared or created within the method, constructor, and block.

Important Points About Local Variable 

  • The scope of local variable is limited, that is it can only be accessed locally which means within method or constructor or only in block level. 
  • Local Variable cannot be accessed using dot(.) operator, or object reference and not even using “this” keyword.
  • Local Variable has to be initialized before the use.
  • It stored in stack memory and has a limited life.
  • Two different local variables present in different scope can have the same name. It’s not considered as Duplication.

Global Variable

The variable whose scope is not limited up to method, constructor, or block level. A global variable can be accessed from anywhere.

Types of Global Variable

  1. Static Variable
  2. Non Static Variable

Static Variable

  1.  Variables which are declared with static keyword are known as static variables. 
  2. These static variables must be created within the class but outside the method body or block or constructor.
  3. Static variables get loaded in the memory (a class area or static pool) once the class (.class file ) is loaded by JVM.
  4. Static variables are also known as class variables. 
  5. Static variables can be directly accessed from both static area and instance area.
  6. Static variables should be accessed by using the class name. However, these variables can also be accessed by using the object reference variable, but it is not recommended. 
  7. If the static variable is modified, then all the referring objects will have the updated value, because, static means one single copy in the memory.
  8.  The scope of the static variable is the same as the scope of the class. That is, once the class is unloaded (–> due to JVM shutdown), the life of static variable will also be expired.
  9.  If we want common data to be shared across multiple objects, then we declare these data members as static variables. 

Non Static Variable

A non-static variable is an Object variable or instance variable which is declared directly inside the class but outside the method, constructor, and block.

Important points regarding Non Static Variable

  1. An instance variable is only created in the memory when an object is created.
  2. It consumed space in Heap Memory as a part of the Object.
  3. The number of copies of each instance variable depends on the number of objects created for a particular class. 
  4. An instance variable can be initialized with default value depends upon datatype.
  5. The life span of the instance variable is the same as of Object, which is instance variable gets created when the object created and destroyed when the object removed.
  6. An instance variable can be accessed using the dot operator throughout the class either by using this keyword or by using object reference. And also can be accessed outside the class by using Object Reference.

Why non static variable is called Instance Variable?

Because when the Object gets created then only instance variable comes into the existence, otherwise if you simply declare in the class and that class does not have an object, then there is no meaning of the variable present in the class.

So once the Object will create then only you are able to do use that variable. So the life of instance variable completely depends upon the Object. Once the object gets destroy then that variable also gets deleted from the memory.

What is a Static Method?

  1. It is also called a class method, which means the static method belongs to a class, not an Object.
  2. When a method does not exhibits Object-specific behaviour or polymorphism behaviour then declare the method to be static.
  3. A static method cannot be inherited, hence it cannot be overridden.
  4. The static method is loaded to memory at the time of class loading.
  5. The static method is also called a Utility Method.

Note: Static method of the superclass can still be accessed using subclass name.

Why the main method is static?

  1. The main method does not exhibit polymorphic behaviour.
  2. It is loaded in the memory at the time of class loading.
  3. JVM can call the main method without the creation of the Object.

Important Points About Main Method in Java

  1. Without main method compilation of program can be done but for execution or running the program, we need the main method.
  2. We can write main method in any class or separate class.
  3. The main method should not be within any other method.
  4. Within the main method, we cannot create any other method.

Note: In Java, we cannot define any method inside the method, that is nested method is not supported by Java.

Rules for Defining the Method in Java

  1. Void method does not have any return statement.
  2. A method can have only one return type.
  3. Return Type and returning value must match.
  4. In the return statement, we can only return one date.
  5. Return statement must be the last executable statement in Method.

Note: We can return multiple data either by using collections or Arrays.

Variable Shadowing

An instance variable and the local variable can have the same name, and in this case inside the local scope, the local variable dominate(shadow) over the instance variable. That means priority of the local variable becomes high. This concept is called Variable shadowing.

Thank You for reading this article. You can check out my other articles on Abstraction, Encapsulation, Polymorphism, and many more. If you any queries or doubt please don’t hesitate to ask, I love to clear your all doubts.

And if you have any suggestions regarding my posts Please let me know.  If you want to learn some other concept of Java, please check out the articles list.

Leave a Reply

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