Monday, August 26, 2019

Java Variables Types : Local Variables, Instance (non-static Variables) & Class (static) Variables


Variables Types in Java: There are 3 types of Variables in Java:


  1. Local Variables
  2. Instance (non Static) variables
  3. Non-Instance/ Class (Static) Variables


1. Local Variables: Variables which are declared Inside methods or constructor are called as local variables for that method or constructor in java.

  • Variables declared Inside method or constructor are limited for that method or constructor only.You cannot access to It outside that method or constructor.
  • You cannot use access modifiers with local variables because they are limited to that method or constructor block only In java software development language.
  • Need to initialize the value of local variables before using It because there Is not any default value assigned to local variables.


Example: in the below example variable num life starts when we enter into the constructor and life ends when we come out of constructor. And same with variables a,b & c. these variables are restricted to use only inside the Sum() method



package testCases;

public class LocalVarExample {
           
            LocalVarExample()
            {
                        int num=10;// Local Variable, we cannot use this in any other method or constructor
                        System.out.println(num);
            }
           
            public void Sum()
            {
                        int a=10,b=20,c; ;// Local Variables

                        c=a+b;
                        System.out.println(c);
            }

            public static void main(String[] args) {

                        LocalVarExample local=new LocalVarExample();
                        local.Sum();

            }
}

Output:
10
30

2. Instance Variables (Non-static Variables): 

Instance variables also called as non-static variables.

  • Instance variables are declared on class level which is parallel to methods or constructors (outside the method or constructor block).
  • Instance variables are generally used with objects. So they are created and destroyed with object creation and destruction In java software development language.
  • Instance(non-static) variables are accessible directly by all the non-static methods and constructors of that class.
  • If you want to access Instance(non-static) variables Inside static method, You needs to create object of that class.
  • Instance variables are always Initialized with Its default values based on Its data types.
  • You can access Instance variable directly (by Its name) Inside same class.
  •  If you want to access It outside the class, then you have to provide object reference with variable name.


Example:
package testCases;

public class InstanceVariablesExample {

            int a=10,b=20,c=0; // Instance variables (non-static variables)
           
            public void Sum()
            {
                       
                        c=a+b;
                        System.out.println(c);
            }

            public static void main(String[] args) {

                        InstanceVariablesExample local=new InstanceVariablesExample();
                        local.Sum();

            }
}

Output:

30

In the above program, variable a,b & c are instance variables. Always we need use object reference to call method or variables

We cannot use non-static(instance variables) in static method. It will give compile time error.

 Below example illustrate the using not-static variable in static method:
package testCases;

public class InstanceVariablesExample_1 {

   int a=10,b=20; // Instance variables (non-static variables)
           
            public static void Sum()
            {
                       
                        int c=a+b;
                        System.out.println(c);
            }

            public static void main(String[] args) {

                        Sum();

            }
}



Output:


Exception in thread "main" java.lang.Error: Unresolved compilation problems:
            Cannot make a static reference to the non-static field a
            Cannot make a static reference to the non-static field b

            at testCases.InstanceVariablesExample_1.Sum(InstanceVariablesExample_1.java:10)
            at testCases.InstanceVariablesExample_1.main(InstanceVariablesExample_1.java:16)
3. Class Variables(Static Variable): class variables are also called as static variables

  • Same as Instance variables, Class variables are declared on class level (outside the method or constructor block). Only difference Is class variables are declared using static keyword.
  • Class variables are used In rare case like when It Is predefined that value of variable will never change. In that case we can use class variables.
  • Class variables are created on program start and destroyed on program end.
  • Class variables are visible to all methods and constructors of class. Class variables are visible to other class based on Its access modifiers. Generally they are public.
  • We can access class variables directly using Its name Inside same class. If you wants to access It outside the class then you need to use class name with variable.

Example:

package testCases;

public class StaticVarExample {
            static int a; //Static variable
            static int b;// static variable
           
            public static void sum()
            {
                        int c=a+b;// we can use static variable inside the static method
                        System.out.println("Sum : "+c);
            }
           
            public void sub()
            {
                        int d=a-b;// we can use static variable inside the non-static method
                        System.out.println("Sub : "+d);
            }
           
            public static void main(String args[])
            {
                        a=30;
                        b=20;
                        sum();
                        StaticVarExample stat=new StaticVarExample();
                        stat.sub();
                       
            }

}

Output:
Sum : 50
Sub : 10



No comments:

Post a Comment

If any suggestions or issue, please provide