Static In Java!

Static in dictionary means stable,unchanging or inactive.

In java static is generally used in case of variables,Methods,blocks and classes.
It is not associated with any instance of the class that is why you can access the static member without creating any instance of the class.


1.Static variables:

 When a class has static variables,those are likely to be called class variables.They don't need instances of the class to be created and then values to be initialized seperately.These can be directly accessed by className.variableName.

When you have the common data for all the objects/instances. Then you might be allocating an extra memory for the same variable for every instance you create.
Ex1:
When a company Xyz has its employee data stored in a class.he knew that everyone are of the same company . Here it creates an extra allocation for all the instances.
public class Employee(){
String Name;
String Company;
long Salary;
Employee(String Name,long Salary;){
  this.Name = Name;
  this.Company="xyz";
  this.Salary= Salary;
}
}
Instead it can be declared static and can be directly accessed by the ClassName.
public class Employee(){
String Name;
private static String Company= "xy;
long Salary;
Employee(String Name,long Salary;){
  this.Name = Name;
  this.Salary= Salary;
}
}

Ex:2
Counter example
public class Student(){
int studentId;
String name;
static int count;
 Student(){
   Count++;
}
}
Above example will have only one count variable for entire class and increments the same whenever new student is created.


2.Static methods:

Static methods are also called class methods.These can be directly accessed without any instance of the class.
Limitations for these static methods are-
*Static methods can use only static variables of the class.This is because it doesn't need any instantiation before its call,so does the static variables.
*static methods cannot refer to this object.This is because it cannot refer to any objects that needed to be initiated.

Why main method is declared static?
The public static keywords mean the Java virtual machine (JVM) interpreter can call the program's main method to start the program (public) without creating an instance of the class (static).


3.Static blocks
Is there any way to run our code without main method?
Yes,but only less than java 1.7.From 1.7 the support is removed.
Static blocks are mainly used to initialize static variables.

Public class Employee(){
 static int employeeCount;
 private String name;

 Employee(String name){
  this.name=name;
  employeeCount++;
 }
 static{
  employeeCount =50;
  System.out.println("employee count initially is 50 employees");
 }
 public static void main(){
  Employee Emp1= new Employee("Ben");
  Employee Emp2= new Employee("Rose");
  Employee Emp3= new Employee("Kary");
   System.out.println("new employee count is :"+ employeeCount );
}

Output :
employee count initially is 50 employees
new employee count is :53
---------------------------------------------
Here the count gets incremented whenever the new employee is added and employeeCount which is initially updated to 50 through a static block and incremented for every new instance of Employee.


4.Static nested classes
Nested Class is a member of its enclosing class.These are of two types.Static nested class and non-static nested class(also known as inner class).Inner class have access to other members of the enclosing class whereas Static nested class cannot have any access to the other members of the enclosing class.
Instances of such classes cannot be created directly but only through the className;
Ex:
class OuterClass {
    ...
    static class StaticNestedClass {
        ...
    }
    class InnerClass {
        ...
    }
}
Static Nested Class can be accessed by OuterClass.StaticNestedClass.New Object can be created by
OuterClass.StaticNestedClass obj = new OuterClass.StaticNestedClass();

Comments

Popular posts from this blog

Sorting Algotithms

Bitwise right shift operators in Java

Program to to Merge Two sorted Linked lists in Java