Thursday, June 28, 2012

5.JAVA BASICS..CONCEPT OF STATIC

Here you will learn about the static keyword which in itself a very useful concept.

What do you understand by static? 
  By seeing the word static three things come to our mind
 1) Class Global variable- It means that it is global to the class. No object copy.
 2) One copy in memory - Only one copy exists in memory.
 3) It exists in memory even before creation of objects.

We will take some examples of objects and their memory allocation.

class Test
{
int a;
float b;
int d=10;
}

class TestMain
{
public static void main(String[] args)
{
Test obj1=new Test();
Test obj2=new Test();
Test obj3=new Test();
obj3.d=15;
obj3.b=5.0;
obj2.a=5;
obj3=new Test();       //Line 7
obj3.d=16;
}
}
Following is the heap diagram with objects.

In the above figure you will find that all the objects are created in the heap memory. obj1,obj2,obj3 are the references which points to these objects. The references obj1,obj2,obj3 are defined inside the method, we can say that these references are using stack memory. In the above objects,you will also  find that all the objects have their own copy of variables. Each object have different value of variables. In short all objects have their own variables.
Obj3 firstly refers to other object and after line-7 it points to different object.

Example of static:
class Test
{
int a;
float b;
static int c
int d=10;
}

class TestMain
{
public static void main(String[] args)
{
Test obj1=new Test();
Test obj2=new Test();
Test obj3=new Test();
obj3.d=15;
obj3.b=5.0;
obj2.a=5;
obj3=new Test();       //Line 7
obj3.d=16
obj1.c=6;
obj3.c=10;
obj2.c=15;
Test.c=20;
}
}

In the above example 'c' is the static variable,so there exists only one copy of variable c and is not inside any of the object area,instead it has a separate memory space which is accessible by any of the objects and even by the class. This is how the static works.

static in broader terms:

Following are the different ways of using keyword static
  • Variables
  • Methods
  • Classes
  • Block

Variables:
A static variable have one copy and any instance can change its value.
Use of Static variables: Whenever you fell a resource should be accessible by all the instances and you need only one copy. Take a simple example of a basket of chocolates, take 'N' students who can take chocolate from this basket.Student will act as an object. When a student takes a chocolate from basket then the number of chocolates decreases. In these types of examples, take count_of_chocolate as a static variable and when an object takes it the count of chocolate decreases.

Static variables exist in memory even before the instances are created. By this statement don't misunderstand that it will always exist in memory. It will only exist in memory when a class gets loaded( you will learn class loading later).

Difference between static variables and normal variables
  • static variables- Class global
    normal variables- Object specific
  • static variables- Only one copy
    normal variables- copies are equal to the number of objects that exist in memory
  • static variables- Exist when class gets loaded
    normal variables- Exist when instance get created, each object has different copy
  • static variables- Every object have access to this variable,also accessed by class name
    normal variables- Object able to access its own copy.

Static methods:
Prefixing static before a method name makes a method static.
Where the normal methods exist in memory?
There is only one copy of normal methods,it does not depend on the number of objects. The methods are stored in Method area. Method area is a separate memory in Heap which stores the class methods.Methods are executed from this area.
Static Methods- They are also stored in the same area and only one copy exist in memory.

Difference between static method and normal method
Both have one copy in memory. Static method exist in memory even when there is no object while the normal method exist only after instance is created.
Can static method access non static variable???..
No. Static method and variable exist in memory when a class gets loaded while the non static variable and method exist only after instance is created. This is the reason why static method cannot access non static variable.

Static Classes:
Remember an outer class can never be static as there is no point in declaring an outer class an static class. Only the nested classes are allowed to be static. Technically, inner classes are not called static, inner classes are local and anonymous classes. Static classes are same as normal classes, the only difference is that static classes cannot access not static member variables and methods of the parent class.
Static class behaves like normal classes.
  Nested Classes: Nested classes are of 2 types
  a) static nested classes
  b) Non-static Nested Classes- These classes are called Inner Class.

To know more about Inner and nested class please read http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Static Blocks:
A class can have number of static block and normal block.
All normal block will be executed each time a instance is created in the order they are exist in class and are executed before the constructor is called while the static block is executed only once on class loading (it's execution does not depend on the number of instance). As the static block is executed on class loading, it is executed before normal blocks gets executed.

Here is a simple example to understand the difference between static and normal block

public class StaticBlockExp {

 static{
  System.out.println("1.static block");
 }
 static{
  System.out.println("2.static block");
 }
 {
  System.out.println("Welcome to Java");
 }
 {
  System.out.println("Welcome to world of java");
 }
 public StaticBlockExp()
 {
  System.out.println("Constructor");
 }
 public static void main(String[] args) {
  new StaticBlockExp();
  new StaticBlockExp();
 }
}

Output of above program:
1.static block
2.static block
Welcome to Java
Welcome to world of java
Constructor
Welcome to Java
Welcome to world of java
Constructor

From this it is clear that static blocks are executed only once while normal blocks are executed each time an instance is created.


2 comments:

  1. nice explanation...

    ReplyDelete
  2. static variables- Class global
    normal variables- Object specific

    Can you explain this in broader term.
    As, If I have a static variable inside a function only, than also it will be treated as GLOBAL??

    ReplyDelete