Static in Java





Static Members :

  • Class members prefix with non-access modifier static, are called static members.

  • Static members are not particular to objects.

  • Most often, these represent public resource (data) like DB connection or services (methods) common to all object of the class.

  • They are accessed with just class name.

Example:

class Math
{
  public static final float PI = 3.14F;
}
To access PI, we don not need to instantiate Math. We will just use the following :-
System.out.println(Math.PI);

A class can have the following type of static members :
  • Static data fields (either variable or constants)
  • Static methods
  • Static block
  • Static nested class

Static Variable :

  • A variable marked as static is known as static variable.

  • It is also refer as class variable.

  • Static variable gets memory only once in class area at the time of class loading, and destroys when the program stops.

  • There would only be one copy of each class variable(static variable) per class, regardless of how many objects are created from it.

  • It is useful when we want to share a common property to all objects.

Example :

class Demo
{
 int x;
 static int y;
public static void main(String []arg)
 {
  Demo ob1 = new Demo();
  Demo ob2 = new Demo();
 }
}

 Here y is static variable of integer type in class Demo.
 There will be a single copy of y created for all instance of Demo class.
prowessapps.in
class Demo
{
 int x;
 static int y;
 void counter()
 {
   x++;
   y++;
  System.out.println("x :"+x);
  System.out.println("y :"+y);
 }
 public static void main(String []arg)
 {
  Demo ob1 = new Demo();
  ob1.counter();
  Demo ob2 = new Demo();
  ob2.counter();
 }
}           
OUTPUT :
x :1
y :1
x :1
y :2        

Static Methods :

  • A method declared with static modifier is called static method.

  • A static method can access other static members (data or methods) in the same class directly.

  • A static method can’t access non-static members of the class directly. An object is required.

  • In a static method, we can’t use this or super.

  • To call a static method object is not required.

Example

class Demo
{
 int x;
 static int y;
 static void show()
 {
  System.out.println("static method");
  // y is direct accessed
  System.out.println("y :"+y);
 }
 public static void main(String []arg)
 {
  //Object is not required
   Demo.show();
//Direct access in static method in same class
   show();
  // may access with object as well
   Demo ob = new Demo();
   ob.show();// not recommonded
   //for x object is required
  System.out.println("x :"+ob.x);
 }
}

Static Block :

  • It is also called static initializer block.
  • Is used to initialize the static data member.
  • Is automatically execute before main method at the time of class loading.

Example :

class Demo
{
 static{
  System.out.println("static block");
 }
 public static void main(String []arg)
 {
  System.out.println("Main method");
 }
}           
OUTPUT :
static block
Main method

Static initializer block executes only once when class is loaded.

Next topic is instance-members




 





Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved