Programming





Variables in Java

Variables in Java


by Administrator | 25-Mar-2020
Java

Tags:  Variables  Core Java  Static  Local  Instance

Share: 


Variable is the name of the location where the data will be stored while executing the application. To provide the information about the data we need to use data types.

  1. Local variables

Local variables are declared in a method, constructor, or block. When a method is entered, an area is pushed onto the call stack. This area contains slots for each local variable and parameter. When the method is called, the parameter slots are initialized to the parameter values. When the method exits, this area is popped off the stack and the memory becomes available for the next called method. Parameters are essentially local variables that are initialized from the actual parameters. Local variables are not visible outside the method.

Rules for declaring a local variable

  1. If you are defining the variable within the member of the class like block, method or constructor or as the parameter for method/constructor then it is known as a local variable.
  2. Memory for the local variable will be allocated with the corresponding execution block will be invoked by the JVM.
  3. JVM won’t initialize a local variable with a default value so before using the local variable it must be initialized explicitly.
  4. The memory for a local variable will be allocated in the stack of the corresponding member of the class. The scope of the local variable is within the corresponding block where it is declared. After completing the execution of the block, the memory will be de-allocated for the member of the class and for the local variable.
  5. You cannot define two variables with the same name within the same scope.
  6. You can use only the final modifier with the local variable.
  7. You can define the local variable with the same name as class-level variable, at the time of accessing the variable the first priority will be for the local variable.
  1. Instance variables

If you are defining the variable on the class without using a static modifier then it is known as an instance variable. An instance variable is related to an instance (object) of class.

Instance variables are declared in a class, but outside a method. They are also called member or field variables. When an object is allocated in the heap, there is a slot in it for each instance variable value. Therefore an instance variable is created when an object is created and destroyed when the object is destroyed. Visible in all methods and constructors of the defining class should generally be declared private but may be given greater visibility.

Rules for declaring an instance variable

  1. If you are creating multiple objects then for every object, a separate memory will be allocated.
  2. The memory allocated for one object will not be accessed or modified by other objects.
  3. Instance variable cannot be accessed from the static context directly.
  4. To access the instance variable one actual object is required. The memory of the instance variable will be allocated in the heap memory when the object of the class will be created.
  5. If you are trying to access instance members with the null reference then the following runtime exception will be true by the JVM. 

Java.lang.NullPointerException

  1. Class/static variables

If you are defining the variable as the member of the class by using a static modifier then it is known as a static variable.

Class/static variables are declared with the static keyword in a class, but outside a method. There is only one copy per class, regardless of how many objects are created from it. They are stored in static memory. It is rare to use static variables other than declared final and used as either public or private constants.

Rules for declaring a static variable

  1. Static variable is related to the corresponding class (Type). Memory for static variable will be allocated at the time of loading of class. Since the class will be loaded by the JVM only once, so the memory for static variable will be allocated only once.
  2. The same memory will be accessed by multiple object of the class so if static variable value will be modified then it will affect to all the objects.
  3. To access the static member class information is required, actual object is not required.
  4. Static variable can be accessed from static or instance content.
  5. It is not recommendable to use the object reference to access the static member. The static member can be accessed with the class name itself.
  6. Instance variable cannot be accessed from the static content because the memory for instance variable will be allocated at the time of creating the object and static member can be accessed without creating the object. Memory for a static variable will be allocated in the heap memory.
  7. Without creating the object we can access that by using the static.
Example of different Variables
class Varibles {
    int instanceSize10; // Instance Variable
    static int staticSize = 100; // Static Variable
    public static void main(String[] args) {
        int localSize = 0; // Local Variable
    }
}

Comments:


There are no comments.

Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.