The mystery keyword static,final

The keyword static
Most of the new java developers are often confused or find difficulty to understand what  keyword static really is.
Java API is full of static methods and variables.
I will explain it clearly,
There are three main reasons why "static" is used:


  1. To does not bother about creating an object and accessing methods or data,directly calling the variable or method.
  2. To  make the instance variables belonging to whole class rather than binding to a particular object,i.e,if a Class A has static variable a,and A has two instance a1 and a2 with instance variable value then the values are same.To allow sharing of data to all the objects accessing static variable.
  3. For count the number of objects created in the program as static variables are shared among objects.
Where static keyword are used:
  1. For making methods static
  2. For making class variables static also called static variables
  3. For making nested class static.
1.Making methods static:Type static keyword before the method name as:
public static void show()
{
//code..code..code
}
2.Making class variables static:simply type static before the variable name as:
static int age; 
3.static nested class do not have access to other member of the enclosed class
class upper{
//code..
static class nested{
//code...
}
}
static variable vs non-static variable:Non-static instance variables cannot be accessed directly by the name but in case of static variable it is possible.
Program code:
class Static_demo
{//variables defined outside main method
int non_static_variable=10;
static int static_variable=10; 
public static void main(String args[])
{
System.out.println("non-static variable value:"+non_static_variable) //accessing non-static variable
System.out.println("static variable value:"+static_variable) //accessing static variable
}
}
Output:exception will occur something like this "Cannot make a static reference to the non-static field"
Corrected code:
class Static_demo
{//variables defined outside main method
int non_static_variable=10;
static int static_variable=10; 
public static void main(String args[])
{
Static_demo sd=new Static_demo();//creating an object of class
System.out.println("non-static variable value:"+sd.non_static_variable) //accessing non-static variable through dot operator using an object
System.out.println("static variable value:"+static_variable) //accessing static variable
}
}
Output:non-static variable value:10
static variable value:10

static method vs non-static method:
Program code:
class Static_method_demo
{//variables defined outside main method
void non_static_method
{
System.out.println("This can't be accessed directly")
}
void static_method
{
System.out.println("This can be accessed directly")
}
public static void main(String args[])
{
Static_method_demo sd=new Static_demo();//creating an object of class
System.out.println("calling non-static method:"+non_static_method()) //accessing non-static directly
System.out.println("calling static:"+static_method) //accessing static method
}
}
Output:exception will occur something like this "Cannot make a static reference to the non-static field" correct the same way as the above.
Showing static variable remains common for every object created:
Program code:
class Static_demo
{
static int i=0;
int j=0;//non static variable 
Static_demo()//this is a constructor
{
i++;
j++;
System.out.println("static variable value"+i);
System.out.println("non-static variable value"+j);

}
public static void main(String args[])
{
Static_demo s1=new Static_demo();
Static_demo s2=new Static_demo();
Static_demo s3=new Static_demo();
}
}
Output:static variable value:1
non-static variable value:1
static variable value:2
non-static variable value:1
static variable value:3
non-static variable value:1


The keyword final
YOU JUST CAN'T CHANGE FINAL VARIABLE VALUES,EXTEND FINAL CLASS,AND CAN'T OVERRIDE FINAL METHODS.
Reasons why final keyword is used:
1.To disallow change in data that are meant to be constant all the time.For example:final int PI=3.14;final int no_of_months=12;
2.To make a reserve class for storing data,thus data items are accessed within the class and the class cannot be extended(inherited).
3.To prevent overriding of specific methods.
Note that final classes cannot be overrided and thus if there is a need  to keep all methods private,make class as final only,no need to make each method final. 
Showing usability of final keyword:
Program code:
public final class Final_class
{
//declaration of methods and instance variables
public static final String LANGUAGE="Java";
//you can further access this variable within this class

class Child extends Final_class//can't be extended exception will pop up
{
public static void main(String args[])
{
//can access but not change the public methods and variables of class Final_class
}
}

Comments

Popular posts from this blog

Four building blocks of Object oriented programming language

Exceptions and exceptional handling

Lists in java