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.
- Local Variable.
- 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
- Static Variable
- Non Static Variable
Static Variable
- Variables which are declared with static keyword are known as static variables.
- These static variables must be created within the class but outside the method body or block or constructor.
- Static variables get loaded in the memory (a class area or static pool) once the class (.class file ) is loaded by JVM.
- Static variables are also known as class variables.
- Static variables can be directly accessed from both static area and instance area.
- 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.
- 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.
- 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.
- 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
- An instance variable is only created in the memory when an object is created.
- It consumed space in Heap Memory as a part of the Object.
- The number of copies of each instance variable depends on the number of objects created for a particular class.
- An instance variable can be initialized with default value depends upon datatype.
- 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.
- 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?
- It is also called a class method, which means the static method belongs to a class, not an Object.
- When a method does not exhibits Object-specific behaviour or polymorphism behaviour then declare the method to be static.
- A static method cannot be inherited, hence it cannot be overridden.
- The static method is loaded to memory at the time of class loading.
- 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?
- The main method does not exhibit polymorphic behaviour.
- It is loaded in the memory at the time of class loading.
- JVM can call the main method without the creation of the Object.
Important Points About Main Method in Java
- Without main method compilation of program can be done but for execution or running the program, we need the main method.
- We can write main method in any class or separate class.
- The main method should not be within any other method.
- 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
- Void method does not have any return statement.
- A method can have only one return type.
- Return Type and returning value must match.
- In the return statement, we can only return one date.
- 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.