INTRODUCTION :
EXPLANATION:
Hei.. all !! here we are.. !! In this post, I am going to explain the types of variable available..
The variable which is almost common to all programming
languages are
- Local variable and
- Global variable
WHAT IS A LOCAL VARIABLE..??
Yeah.. as soon as you read the heading you would
have guessed the definition .. (I hope :D ) !! Of course ..your definition is
correct.. !!
The variable which is
available locally is called local variable ..!!
But what does it mean
by local ?? :/
Okies.. Lets jump into the example..
EXAMPLE :
Class happy
{
public void add( )
System.out.println( “ My num is “ + sad );
public void add( )
{
int sad = 5 ;
}
public static void
main(String args[])
{
happy ha= new happy( ); // creating an object
ha.add( ); // accessing the method using object
ha.add( ); // accessing the method using object
}
}
OUTPUT:
My num is 5.
Now here.. class
name is happy and the method which is present inside it is add ( ) .
Now the variable ‘sad’ is present inside the method add( ) .
If a variable is present inside any method it is called
‘local variable’. It is locally available to that method alone.
If any method tries to access the variable then it will
throw an error saying the variables and method can’t be accessed directly. The
way to access them is to use the object.
So, in short the variable which is present inside any method
is called ‘local varible’.
WHAT IS A GLOBAL VARIABLE..?
The variable which
is declared globally is called ‘global variable’.
That is,if the
variable is declared outside the method it is called global variable
MEMBER VARIABLE:
There are 3 types of member variable. They are,
- Instance variable
- Static variable and
- Final variable
WHAT IS AN INSTANCE VARIABLE..?
The variable which is declared inside the class but outside
the method is called ‘instance variable’
EXAMPLE:
Class Happiest
{
int happier = 5 ;
System.out.println( “ My num is “ + happier );
int happier = 5 ;
public void happy( )
{
}
public static void
main(String args[])
{
Happiest ha = new Happiest( );
ha.happy( );
ha.happy( );
}
}
EXPLANATION:
Here ‘happier’ is the
variable here an ‘instance variable’. This variable is declared inside the
class ‘Happiest’ but it is declared outside the method ‘happy’
This ‘ happier’ variable is tooo happy because this is
available through out the class.
DIFFERENCE BETWEEN INSTANCE VARIABLE AND LOCAL VARIABLE:
The main difference between local and instance variable is,
If the local variable is not initialized it will throw out
the error but it we fail to initialize ‘INSTANCE VARIABLE’ it will get
initialized on its own.
EXAMPLE:
Class Happiest
{
int happier ;
System.out.println( “ My num is “ + happier);
int happier ;
public void happy( )
{
}
public static void
main(String args[])
{
Happiest ha = new Happiest( );
ha.happy( );
ha.happy( );
}
}
OUTPUT:
0
EXPLANATION:
The output is 0 and it didn’t throw any error because it is
an ‘INSTANCE VARIABLE’.
If we didn’t initialize or assign any value to it , it will
assign the default value 0 for Integer data type and ‘null’ for string
datatype.
EXAMPLE FOR LOCAL VARIABLE:
Class Happiest
{
public void happy( )
System.out.println( “ My num is “ + happier);
public void happy( )
{
int happier ;
}
public static void
main(String args[])
{
Happiest ha = new Happiest( );
ha.happy( );
ha.happy( );
}
}
OUTPUT:
Compile time error. The local variable ‘happier’ is without
initialization and it will throw error to initialize it.
STATIC VARIABLES:
Static variables are independent of objects meaning they
don’t belong to specific objects.
Only one copy of ‘static variable’ exists and they will be
shared among all of the objects.
Static variables are rare . They are unique and have more
demand and so only copy exists for a class ; )
DOES STATIC VARIABLE HAVE AN ALTERNATE NAME.. ??
Yes … static variable is also called as ‘CLASS VARIABLE’ as
they belong to the entire class and not
to any specific object.
The static variable won’t see any partiality .It wont think
like.. Oh..man !! This object looks good and polite and so I will be available
only to them…” :D
Instead.. I am one and I will be available to all the
objects. There is no partiality when it comes to availability is what this
‘static variable’ thinks..
EXAMPLE:
class StaticExample
{
static int count=0;
public void increment()
{
count++;
}
public static void main(String args[])
{
{
StaticExample obj1=new StaticExample();
StaticExample obj2=new StaticExample ();
obj1.increment();
obj2.increment();
System.out.println("Obj1 count
is="+obj1.count);
System.out.println("Obj2 count
is="+obj2.count);
}
}
OUTPUT:
Obj1 count is
= 2
Obj2 count is=
2
EXPLANATION:
Look at the above code.. obj1 and obj2 are the 2 objects
created for the class StaticExample.
When the method increment ( ) is called for the first time
ie. obj1.increment ( ) then the method gets called and increments the value of
the “count “
The value of the count is now 1.
NOTE: COUNT IS A STATIC VARIABLE and so only one copy of the
variable exists.
Then when the same method is called using DIFFERENT OBJECT
the value is incremented again and now the value of the count now turns to 2.
BIT COMPLEX EXAMPLE:
Class staticexample
{
static int X= 10;
int Y=5; // non static variable
public static void main(String [] args)
{
staticDemo instance1 = new StaticDemo(); // creating object for a class
staticDemo instance2 = new StaticDemo(); // creating another object for a class
//printing the value of x and y using 1st object
System.out.println("instance1.X=" + instance1.X+ "instance1.Y="+instance1.Y);
//printing the value of x and y using 1st object
System.out.println("instance2.X=" + instance2.X+ "instance2.Y="+instance2.Y);
instance1.X=15; // changing the value of x and y
instance1.Y=10;
System.out.println("After updating X value to 15 and Y value to 10 from instance1 : ");
System.out.println("instance1.X="+instance1.X+"instance1.Y="+instance1.Y "); //explained below
System.out.println("instance2.X="+instance2.X+"instance2.Y="+instance2.Y ");
//explained below
}
}
OUTPUT:
instance1.X = 10 instance1.Y=5
instance2.X = 10 instance2.Y=5
After updating X value to 15 and Y value to 10 from instance1:
instance1.X=15 instance1.Y=10
instance2.X=15 instance2.Y=5
{
static int X= 10;
int Y=5; // non static variable
public static void main(String [] args)
{
staticDemo instance1 = new StaticDemo(); // creating object for a class
staticDemo instance2 = new StaticDemo(); // creating another object for a class
//printing the value of x and y using 1st object
System.out.println("instance1.X=" + instance1.X+ "instance1.Y="+instance1.Y);
//printing the value of x and y using 1st object
System.out.println("instance2.X=" + instance2.X+ "instance2.Y="+instance2.Y);
instance1.X=15; // changing the value of x and y
System.out.println("After updating X value to 15 and Y value to 10 from instance1 : ");
System.out.println("instance1.X="+instance1.X+"instance1.Y="+instance1.Y "); //explained below
System.out.println("instance2.X="+instance2.X+"instance2.Y="+instance2.Y ");
//explained below
}
}
OUTPUT:
instance1.X = 10 instance1.Y=5
instance2.X = 10 instance2.Y=5
After updating X value to 15 and Y value to 10 from instance1:
instance1.X=15 instance1.Y=10
instance2.X=15 instance2.Y=5
Initially the values are 10 and 5
.
Then the object ‘instance 1’ is accessing the variable x and y and trying to
change the values.
It is changing the value of x and y to 15 and 10.
Note that for static variable there is only one copy and so when any changes happen to it will be reflected to all the objects.
Note that for static variable there is only one copy and so when any changes happen to it will be reflected to all the objects.
So the variable x erases its
previous memory 10 and now updates its new value 15.
But lucky variable ‘Y’ has
made itself change its value only for
object ‘instance 1’ ie. So, instance 1 has the variable value Y = 10 and
instance 2 =5.
Wow.. Instance 2 has the value Y=
5 and not 10 which is the updated value because Y is normal variable and only
the instance 1 object has changed its variable’s value and not the instance 2.
When value changes to 15 then the
initial value will get deleted from the variable’s memory and so VALUE = 15
will be available to both the objects where as for NORMAL VARIABLE
A
copy of variable is available to each object and note we are changing the value
of variables that is belonging to object instance1 and so the values resides
within that object itself
Unless until the variable is
static, each object will have its own set of values.
FINAL VARIABLES:
When we apply "final"
keyword to a variable,the
value of that variable remains constant. (or) Once we declare a variable as final,the value
of that variable cannot be changed.
EXAMPLE:
Class Ex
{
public final int a= 10;
Class Ex
{
public final int a= 10;
public void add()
{
Int a= 12;
System.out.println(“the value of a = “ +a);
}
public static void main(String
args[])
{
Ex e = new Ex( );
e.add( );
e.add( );
}
OUTPUT:
Compile time error.
EXPLANATION:
This is because we are trying to change the value of ‘a’
inside the method.
‘a’ is a final variable and so it can’t be changed.
DIFFERENCE BETWEEN STATIC AND FINAL KEYWORD:
When a variable is
declared ‘STATIC’ only one copy exists and it belongs to entire class .
Whereas when a FINAL
VARIABLE is used then the value of a variable cannot be changed inside the
program no matter what.
CONCLUSION:
So, that's it for this post. We have seen a lot more about variable. I am sure you people will be clear about the concepts. If you have any doubts, post in the comments below. Your feedback is welcomed. Help me improve this blog with your valuable feedback. Hit like button and share this post if you find it useful. Have a pleasant day :)
CONCLUSION:
So, that's it for this post. We have seen a lot more about variable. I am sure you people will be clear about the concepts. If you have any doubts, post in the comments below. Your feedback is welcomed. Help me improve this blog with your valuable feedback. Hit like button and share this post if you find it useful. Have a pleasant day :)
No comments :
Post a Comment