Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Java Class
Java Object
Java Object Array
Java Nesting of Classes
Java Methods
Java Method Overloading
Java Constructor
Java Constructor Overloading
Java This Keyword
Java Garbage Collection
Java Static Keyword
Java Arrays
Java String
Java StringBuffer
Java String vs StringBuffer
Java Vector Class
Java Vector vs Array
Java Wrapper Classes

Inheritance

Java Inheritance
Java Single Inheritance
Java Super Keyword
Java Multiple Inheritance
Java Hierarchical Inheritance
Java Multilevel Inheritance
Java Method Overriding
Java Abstract Classes,Methods
Java Final Variables, Methods
Java Dynamic Dispatch Methods
Java Visibility Controls

Interfaces

Java Interface
Java Class vs Interface
Java Multiple Inheritance
Java Nested Interface

Packages

Java Packages
Java Built in packages
Java Package Access Specifiers

Exception Handling

Multithreading

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Static Keyword


Static Members

Static keyword is used to make static members. The static members are associated with class itself rather than individual objects. They are also referred as class variables and class methods.

Normally the class contains two sections as

  1. Declaration of variables - called as instance variables
  2. Declaration of methods - called as instance methods

They are called as instances of class because every time when object is created then a new copy of variables and methods are created which are accessed using . operator.

When we want to define members which are common to all objects and access without using . operator then that members belongs to the class rather than objects.


Characteristics of static members

  1. They are declared using static keyword
  2. Static variables are initialized to zero before creating first object
  3. Static members are called by directly class name instead of object
  4. Only one copy of static variable is created and shared to all objects
  5. Static methods called by itself without depending on any object
  6. Static methods can call only static methods and access only static type of data
  7. Static methods cannot refer to this or super

Syntax

class ClassName
{
	static int varName;	// static field declaration
	static returnType methodName(parameters list);	// static method declaration
}

The public static void main() method inside class is also a static method in java. It is called without creating an object of that class.

Example - without using static variable

class WithoutStatic
{
    int counter = 0;
    WithoutStatic()
    {
	counter ++;
	show();
    }
    void show()
    {
	System.out.println("Counter =  "+counter);
    }
    public static void main(String args[])
    {
	WithoutStatic n1,n2,n3,n4,n5;
	n1 = new WithoutStatic();
	n2 = new WithoutStatic();
	n3 = new WithoutStatic();
	n4 = new WithoutStatic();
	n5 = new WithoutStatic();
    }
}

Output

Counter = 1
Counter = 1
Counter = 1
Counter = 1
Counter = 1

In above example we have created 5 objects of WithoutStatic class. Every time default constructor is called then counter is incremented from 0 to 1 but, the incremented value is not shared to all objects hence every objects gets the value 1.


Example - Using static variable

Static keyword
class UsingStatic
{
    static int counter;	// auto initialized to zero
    UsingStatic()
    {
	counter ++;
	show();
    }
    void show()
    {
	System.out.println("Counter =  "+counter);
    }
    public static void main(String args[])
    {
	UsingStatic n1,n2,n3,n4,n5;
	n1 = new UsingStatic();
	n2 = new UsingStatic();
	n3 = new UsingStatic();
	n4 = new UsingStatic();
	n5 = new UsingStatic();
    }
}

Output

Counter = 1
Counter = 2
Counter = 3
Counter = 4
Counter = 5

In above example we have declared counter variable as static.When first object is created then it is automatically initialized to zero. The default constructor increments the value of counter by 1 and immediately shares to all objects.The new objects starts to increments counter from previous value.


<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo