Java static keyword

The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.
It can be used along with Class name, Variables, Methods and block.

  1. static class (nested class)
  2. static block
  3. static methods
  4. static variables

Static Class

A Class can be made static only if it is a nested Class. The nested static class can be accessed without having an object of outer class.

Example 1:

class Example1{
  //Static class
  static class X{
      static String str="Inside Class X";
  }
  public static void main(String args[])
  {
      X.str="Inside Class Example1";
      System.out.println("String stored in str is- "+ X.str);
  }
}

Static Block

Static block is mostly used for changing the default values of static variables.This block gets executed when the class is loaded in the memory.

A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program.

Example 

class program
{
   static int num;
   static String mystr;
   static
   {
    num = 97;
      mystr = "Static keyword in Java";
   }
   public static void main(String args[])
   {
      System.out.println("Value of num="+num);
      System.out.println("Value of mystr="+mystr);
   }
}

Static Methods

Static Methods can access class variables without using object of the class. It can access non-static methods and non-static variables by using objects. Static methods can be accessed directly in static and non-static methods.

Example 

class program
{
   static int i;
   static String s;
   public static void main(String args[]) //Its a Static Method
   {
       Example5 obj=new Example5();
       //Non Static variables accessed using object obj
       System.out.println("i:"+obj.i);
       System.out.println("s:"+obj.s);
   }
}

Static variables:

If you declare any variable as static , it is known as static variable.

The static variable can be used to differ the common property of all objects like company name of employees , college name of students.

The static variables gets memory only once. In class area at the time of class loading.


Advantages of stativ variables :

It makes your program memory efficient.(it saves memory) understanding without static variable.

Class  student 
{
Int roll no;
String name;
String college=”LEARN”;
}

Suppose there are 500 students in my college now all instant data members all created . All students each time when object is name so instant data member is good . Here college refers to the common property of all objects. If we make it static ,this feils will get memory only once.


Example of Static variable:

import java.io.*;
class Student
{
Int  roll no;
Static String college=”LEARN”
Student (int r  String n)
{
Roll no=r;
Name=n;
}
System.out.println(roll no=+””+name+””+college);
}
Public static void main (String  aaaargs[])
{
Student s1=new Student(101,”VEERU”);
Student s2=new Student(102,”GOPI”);
S1,disply();
S2,disply();
}
}

A sample Counter program of without static variable

class Counter
{  
int count=0  
Counter()
{  
count++;  
System.out.println(count);  
}    
public static void main(String args[])
{  
Counter c1=new Counter();  
Counter c2=new Counter();  
Counter c3=new Counter();  
 }  
}  

A sample Counter program of with static variable

class Counter2
{  
static int count=0;  
Counter2()
{  
count++;  
System.out.println(count);  
}  
public static void main(String args[]){    
Counter2 c1=new Counter2();  
Counter2 c2=new Counter2();  
Counter2 c3=new Counter2();    
}  
}  

News Walls


Copyrights © 2016. All Rights Reserved to Learn Computers Home, India. //Site Designed.