Variable or data member in java

Variable/Data member:-

The variable or data member in java is a block of memory which can store a data value. In Java, all the variable should be declared before they can be used.

Java support 6 types of variable

  1. Local Variable
  2. Static Variable
  3. Non-static Variable
  4. Final Variable
  5. Transient Variable
  6. Volatile Variable

Local Variable

  • The variable declared within a block is known as local variable or the function parameter is local.
  • Local variable never stores default variable datatype. We can be called within the same block.
  • If we use the local variable it must be initialize it.
  • A local variable can be accessed within the same block.

PROGRAM

class Local

{

        void fun(int z)

        {

                        z=200;   //Local variable

                        System.out.println(z);

        }

        public static void main(String[] args) 

        {

                        int x;

                        int y=100;            //Local variable

                        System.out.println(y);

                        Local t=new Local();

                        t.fun(10);             //calling the fun function

        }

}

O/P

             200

 Static Variable

  • The variable declared with the static keyword is known as the static variable.
  • We can be called the static variable directly, by class name and by object name within the same class, but outside the class, the static variable can be called only through class name and object name but not directly.
  • Static creates the single copy of the variable in the whole program.
  • The static variable holds the default value of the datatype.

PROGRAM

class Demo

{

                static int x=20;  //static variable

                

                public static void main(String[] args) 

                {

                                System.out.println(x);                  //call directly

                                System.out.println(Demo.x);     //call by class name

                                Demo d=new Demo();

                                System.out.println(d.x);              //call by object name

                

                }

}

O/P  20              

            20

            20

Non-static Variable

  • The variable declared without static keyword is known as non-static variable or object type variable.
  • The non-static variable only can be called through object name.
  • Non-static creates a different copy from the different object.

PROGRAM

class Demo

{

         int x=20;             //nonstatic variable

        

        public static void main(String[] args) 

        {

                        Demo d=new Demo(); //call through object

                        System.out.println(d.x);

        

        }

}

O/P 20

Final Variable:-

  • The variable declared with the final keyword is known as the final variable.
  • It is always constant i.e the value of the variable cannot be changed.
  • At the time of declaration final variable should be initialized.
  • Final variable can be declared as static or non-static.
  • To avoid memory wastage static can be used.

PROGRAM:-

class Demo

{

         final int x=10;

         final static int y=20;

                        

        public static void main(String[] args) 

        {

                                        Demo d=new Demo();

                                        System.out.println(d.x);

                                        System.out.println(y);

        }

}

O/P  10

             20
  • Transient Variable
  • Volatile Variable
basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community

2 thoughts on “Variable or data member in java

Leave a Reply

Skip to toolbar