JAVA VARIABLES

 

 

In  Java, variables are storage containers which  store  any type of input  values .  Understanding   java  variable  is  important  to learn java programming.

 

 

Main Components  of  Variables in Java:

A  variable  in   java   has   3  components:

  • Data Type:Defines the  kind  of  data  stored (e.g., int,double,float,String)
  • Variable  Name: A  unique  identifier following  the java  naming  rules.
  • Value: The  actual  data  assigned to  the  variable.

 

 

2  RULES  TO  USE  VARIABLES  IN  JAVA  APPLICATION:

 

(1) Variable Declaration

(2) Variable  Initialization

 

 

 

 

 

 

How to  declare  a  variable  in   Java?

To  declare  a  variable  in  java , we need to  take care of  two things which are:

 

1.Data type: In java,data type define  the  type  of  data  which a variable  can  hold.

 

2.Variable  Name:  Variable Name must follow  the  java naming  convention.

 

eg:  int   sum;

       double  salary;

 

where int, double  are  data type  &  sum,salary are variable name.

 

 

 

 

 

 

       

 

How  to  initialize Java  Variables?

 

 

We can initialize  Java  Variables  as  given  below:

 

int  sum=88;

 

Where int is  the  data type , sum is  the  variable  name  and  88  is the  value  assigned.

 

 

 

 

 

Types  of  Java  Variables:

 

Different   types  of  Java  variables  are  as  listed  below:

 

(1)Local  Variables

(2) Instance  Variables

(3) Static  Variables

 

(1) Local   Variables: A  variable  defined  within a  block or  method  or  constructor is  called  a  local  variable.

  • Local variables  are  created  at  the time  of  declaration  and  destroyed  when the method  or  function  completed it’s  execution.
  • Local variable need  to  be  initialized  first  before  using  it  within  it’s scope.
  • The scope  of  the  local  variables  exists only  within  the method or  block in which  they are  declared

 

 

 

 

 

Example:  This example  shows  how   a  local  variable  is  declared  and  used  within  the  main  method.

class    A{

public static  void main(String args[]){

//Declared  a  local  variable

int  num=88;

//this  variable  num  is  local  to  the  main  method  only

System.out.println(“Local variable:”+num);

}

}

 

Example:  This  example  shows  how  a    local  variable is  declared  and  used  inside a  normal method  and  it  can’t  be  used  outside  of  it.

class   B{

public static  void  main(String  args[]){

static  int  a=10;

void  display(){

//Declaring  a  local  variable

int b=88;

//this  variable b   is  local  to  the  method  display()  only

System.out.println(“Local variable:”+b);

}

}

}

In the  above  example  variable  b  is   local  to  the  method  display()  only  &  it  can’t  be  access  outside of this  display()  method.

 

 

(2) Instance  Variable:   Instance  variables  are  known as  non-static  variables  which  are  declared  in  a  class  outside  of  any  method,constructor  or  block.

 

  • Instance  variable  of  a  class  is  created  when an object of  the  class  is  created  and  destroyed  when the  object of  the  class  is  destroyed.
  • Initialization  of  instance  variable  is  not  mandatory. It’s  default  value   depend  on  the  data  type  of the  variable. For int, it is 0, for  String it is  null , for  float it  is 0.0f etc.
  • We may  use  access  specifiers  for  instance  variables. If we  don’t specify any  access  specifier, then  default  access  specifier  will  be  used.
  • Scope  of  the  instance  variable are  throughout the class    except  the  static  contexts. For eg: If we use instance  variable  inside the  static main method it will  give  error. If we want to access the  instance variable  inside the static  method, first  we  have  to  create  the  object of the  class , then with the  help of  object  reference  one  can  access  that  instance  variable inside  static main method.
  • Instance  variables   can  be  accessed  by  creating  objects.
  • Instance  variables  can  be  initialized  using  constructors  while creating an  object.  One  can  also  used  instance  blocks  to  initialize  the  instance  variables.

 

Example: This  example  shows  the  use  of  instance  variables  in a  class.

 

import  java.io.*;

 

class   A{

//Declared  instance  variable

public  String name;

public int i;

public   A(){

//Default  constructor

//initializing  instance  variable

this.name=”Joy”;

}

//Declaring main  method

public  static  void  main(String  args[]){

//Object  creation

A    a=new  A();

//Displaying  output

System.out.println(“name  is:”+a.name);

System.out.println(“Default value  for  int is:”+a.i);

 

}

}

 

Output

name  is : Joy

Default value  for  int  is: 0

 

 

 

 

 

 

 

(3) Static  Variables:  Static  variables  are  variables  which  are  declared  with  static  keyword  and also  known as  class  variables.

 

  • Static  variables  are   created  at  the  start  of  program  execution  and  destroyed  automatically  when  execution  ends.
  • One class  can have only one copy of  static  variable , irrespective  of how  many   objects  we  create.
  • Initialization of  static  variable  is  not mandatory. If we  don’t initialize  the  static  variable it will print  the  default  value. For String default  value is null ,  for int  it  is 0 , for float it is 0.0 etc.
  • One  can access  the  static  variables  with  the  help  of  class  name without  creating the object of a class  which  saves  the  memory  of  the  class.
  • Static blocks  can  be  used   to  initialize  the  static  variable.

 

Example:  This  example  describes the  use  of  static  variable, which  belong to  the class  and  can  be  accessed  without  creating an  object  of  the class.

//Java program of  static  variables

 

import  java.io.*;

class A{

//Declaration  of  static  variable

public  static  String  name=”Joy”;

public  static  void  main(String  args[]){

//Displaying  the  name  without  creating the  object  of  class with the help of  class name

System.out.println(“name is:”+A.name);

}

}

 

 

Output:  name is: Joy

 

 

 

 

Instance  Variable  vs  Static  Variables

  1. Static  variables  belong  to  class  itself. Whereas, Instance  variables  belongs  to  a  specific  object of the  class.

        2. Static  variable  allocated  memory  once, at  the  time  of class  is loaded.

             Whereas instance  variables  allocated  memory  for  each  object  of  the  class

       3. Static  variables  are  initialized  only  once  when the class  is  loaded.

             Whereas, instance  variable  initialized  each  time  a  new object  is  created

       4.Static  variables  can  be  easily  accessed  within  a  static  context

            Whereas,instance  variable can’t  be  accessed  within a  static  context  directly without  creating the  object  reference.

       5. Static  variables  can  be  easily  accessed  within a  instance  context or instance method.

           But instance  variable  can  be  accessed  within instance context only , not within static context.

Q1. What  is  variable  in  java?

Answer: Variable  in   java  is  a  storage  container  to  store  any  type  of  supported  input  value by java.

                                                                                                                                                                   Read  more…

Leave a Comment

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