Static is a keyword in java used to represent the class members. It can be used with variable, method, initializer block and nested class.
Types of class members:
- Static data members.
- Static initializer block.
- Static method.
- Static nested class.
1. Static data members:
Data members declared with static keyword are known as static data members. These are mainly used to represent those properties which are common to every object. At the time of class loading a single copy is created for static data members, which is shared by all objects.
Memory division in a java program execution.
In a java program execution memory is divided into three parts:
- Stack.
- Heap.
- Class/Method area.
- Stack: Stack is used to local variables of the methods.
- Heap: Heap is used to store objects.
- Class Area: Class area is used to store static data members.
Static data members are used to represent those properties which are common to every object.
Example:Â
StaticExample1.java
/**
* This program is used to show that static data members are
* used to represent those properties which are common to every object.
* @author W3spoint
*/
class MCAStudent{
//name and rollNo are not common for all students
//so keep them as non-static data members.
String name;
int rollNo;
//As course offered is same for all students
//so keep it as static.
String courseName = "MCA";
//constructor
MCAStudent(String n, int r){
name = n;
rollNo = r;
}
//display all values
public void display(){
System.out.println("Name = " + name);
System.out.println("RollNo. = " + rollNo);
System.out.println("Course Name = " + courseName);
System.out.println("");
}
}
public class StaticExample1 {
public static void main(String args[]){
//create object of MCAStudent class.
MCAStudent stu1 = new MCAStudent("jai", 6);
MCAStudent stu2 = new MCAStudent("sunil", 15);
//method call
stu1.display();
stu2.display();
}
} |
/**
* This program is used to show that static data members are
* used to represent those properties which are common to every object.
* @author W3spoint
*/
class MCAStudent{
//name and rollNo are not common for all students
//so keep them as non-static data members.
String name;
int rollNo;
//As course offered is same for all students
//so keep it as static.
String courseName = "MCA";
//constructor
MCAStudent(String n, int r){
name = n;
rollNo = r;
}
//display all values
public void display(){
System.out.println("Name = " + name);
System.out.println("RollNo. = " + rollNo);
System.out.println("Course Name = " + courseName);
System.out.println("");
}
} public class StaticExample1 {
public static void main(String args[]){
//create object of MCAStudent class.
MCAStudent stu1 = new MCAStudent("jai", 6);
MCAStudent stu2 = new MCAStudent("sunil", 15);
//method call
stu1.display();
stu2.display();
}
}
Output:
Name = jai
RollNo. = 6
Course Name = MCA
Name = sunil
RollNo. = 15
Course Name = MCA |
Name = jai
RollNo. = 6
Course Name = MCA
Name = sunil
RollNo. = 15
Course Name = MCA
Download this example.
Non-static data members have different memory locations for different objects.
Example:
StaticExample2.java
/**
* This program is used to show that non-static data members
* have different memory locations for different objects.
* @author W3spoint
*/
class Test{
int num = 0;
//constructor
Test(){
num = num + 10;
System.out.println("Number = " + num);
}
}
public class StaticExample2 {
public static void main(String args[]){
Test obj1 = new Test();
Test obj2 = new Test();
Test obj3 = new Test();
Test obj4 = new Test();
}
} |
/**
* This program is used to show that non-static data members
* have different memory locations for different objects.
* @author W3spoint
*/
class Test{
int num = 0;
//constructor
Test(){
num = num + 10;
System.out.println("Number = " + num);
}
} public class StaticExample2 {
public static void main(String args[]){
Test obj1 = new Test();
Test obj2 = new Test();
Test obj3 = new Test();
Test obj4 = new Test();
}
}
Output:
Number = 10
Number = 10
Number = 10
Number = 10 |
Number = 10
Number = 10
Number = 10
Number = 10
Download this example.
Static data members use the same memory locations for all objects.
Example:
StaticExample3.java
/**
* This program is used to show that static data members
* use the same memory locations for all objects.
* @author W3spoint
*/
class Test{
static int num = 0;
//constructor
Test(){
num = num + 10;
System.out.println("Number = " + num);
}
}
public class StaticExample3 {
public static void main(String args[]){
Test obj1 = new Test();
Test obj2 = new Test();
Test obj3 = new Test();
Test obj4 = new Test();
}
} |
/**
* This program is used to show that static data members
* use the same memory locations for all objects.
* @author W3spoint
*/
class Test{
static int num = 0;
//constructor
Test(){
num = num + 10;
System.out.println("Number = " + num);
}
} public class StaticExample3 {
public static void main(String args[]){
Test obj1 = new Test();
Test obj2 = new Test();
Test obj3 = new Test();
Test obj4 = new Test();
}
}
Output:
Number = 10
Number = 20
Number = 30
Number = 40 |
Number = 10
Number = 20
Number = 30
Number = 40
Download this example.
Note: Constructor is not used to initialize the static data members because constructor initializes many times but static data members only once. So instead of constructor static initialize block is used to initialize static data members.
Static initializer block: Click here for static initializer block.
Static methods:
Static methods represent the behavior of whole class. An instance of a class is not required to execute static methods. They can be called using class name.
Syntax: ClassName.methodName
Example:
StaticExample4.java
/**
* This program is used to show that there is no need of an object
* for accessing a static method. It can be called with class name.
* @author W3spoint
*/
class Display {
//static method
public static void display(){
System.out.println("Hello w3spoint.com");
}
}
public class StaticExample4 {
public static void main(String args[]){
//No need for object to call static method.
Display.display();
}
} |
/**
* This program is used to show that there is no need of an object
* for accessing a static method. It can be called with class name.
* @author W3spoint
*/
class Display {
//static method
public static void display(){
System.out.println("Hello w3spoint.com");
}
}
public class StaticExample4 {
public static void main(String args[]){
//No need for object to call static method.
Display.display();
}
}
Output:
Download this example.
Note: We can execute a program without main method.
Example:
/**
* This is java program without using main function.
* @author W3spoint
*/
public class WithoutMain {
static{
System.out.println("Hello w3spoint.com");
System.exit(0);
}
} |
/**
* This is java program without using main function.
* @author W3spoint
*/
public class WithoutMain {
static{
System.out.println("Hello w3spoint.com");
System.exit(0);
}
}
Output:
Limitations of static methods and static initialize blocks.
- Non-static data members can’t be accessed in static methods and static initialize blocks.
- Non-static methods can’t be invoked in static methods and static initialize blocks.
- This or super keyword can’t be refers in static methods and static initialize blocks.
1. Non-static data members can’t be accessed in static methods and static initialize blocks.
Example:
StaticExample5.java
/**
* This program is used to show that non-static data members can’t
* be accessed in static methods and static initialize blocks.
* @author W3spoint
*/
class Test {
//non-static data member
int num = 10;
static{
//error because non-static data members can't be
//accessed in static initializer block.
System.out.println("Num = " + num);
}
public static void display(){
System.out.println("Hello w3spoint.com");
//error because non-static data members can't be
//accessed in static method.
System.out.println("Num = " + num);
show();
}
}
public class StaticExample5 {
public static void main(String args[]){
//creating object of Test class
Test obj = new Test();
//method call
obj.display();
}
} |
/**
* This program is used to show that non-static data members can’t
* be accessed in static methods and static initialize blocks.
* @author W3spoint
*/
class Test {
//non-static data member
int num = 10;
static{
//error because non-static data members can't be
//accessed in static initializer block.
System.out.println("Num = " + num);
}
public static void display(){
System.out.println("Hello w3spoint.com");
//error because non-static data members can't be
//accessed in static method.
System.out.println("Num = " + num);
show();
}
} public class StaticExample5 {
public static void main(String args[]){
//creating object of Test class
Test obj = new Test();
//method call
obj.display();
}
}
Output:
Exception in thread "main" java.lang.Error:
Unresolved compilation problems:
Cannot make a static reference to the non-static field num
Cannot make a static reference to the non-static field num
The method show() is undefined for the type Test
at com.w3spoint.business.Test.(StaticExample5.java:15)
at com.w3spoint.business.StaticExample5.main
(StaticExample5.java:31) |
Exception in thread "main" java.lang.Error:
Unresolved compilation problems:
Cannot make a static reference to the non-static field num
Cannot make a static reference to the non-static field num
The method show() is undefined for the type Test
at com.w3spoint.business.Test.(StaticExample5.java:15)
at com.w3spoint.business.StaticExample5.main
(StaticExample5.java:31)
Download this example.
2. Non-static methods can’t be invoked in static methods and static initialize blocks.
Example:
StaticExample6.java
/**
* This program is used to show that non-static methods can’t
* be accessed in static methods and static initialize blocks.
* @author W3spoint
*/
class Test {
//non static method
public void show(){
System.out.println("Hello world.");
}
static{
//error because non-static methods can't be
//accessed in static initializer block.
show();
}
public static void display(){
System.out.println("Hello w3spoint.com");
//error because non-static methods can't be
//accessed in static method.
show();
}
}
public class StaticExample6 {
public static void main(String args[]){
//creating object of Test class
Test obj = new Test();
//method call
obj.display();
}
} |
/**
* This program is used to show that non-static methods can’t
* be accessed in static methods and static initialize blocks.
* @author W3spoint
*/
class Test {
//non static method
public void show(){
System.out.println("Hello world.");
}
static{
//error because non-static methods can't be
//accessed in static initializer block.
show();
}
public static void display(){
System.out.println("Hello w3spoint.com");
//error because non-static methods can't be
//accessed in static method.
show();
}
} public class StaticExample6 {
public static void main(String args[]){
//creating object of Test class
Test obj = new Test();
//method call
obj.display();
}
}
Output:
Exception in thread "main" java.lang.Error:
Unresolved compilation problems:
Cannot make a static reference to the non-static
method show() from the type Test
Cannot make a static reference to the non-static
method show() from the type Test
at com.w3spoint.business.Test.(StaticExample6.java:17)
at com.w3spoint.business.StaticExample6.main
(StaticExample6.java:32) |
Exception in thread "main" java.lang.Error:
Unresolved compilation problems:
Cannot make a static reference to the non-static
method show() from the type Test
Cannot make a static reference to the non-static
method show() from the type Test
at com.w3spoint.business.Test.(StaticExample6.java:17)
at com.w3spoint.business.StaticExample6.main
(StaticExample6.java:32)
Download this example.
3. This or super keyword can’t be refers in static methods and static initialize blocks.
Example:
StaticExample7.java
/**
* This program is used to show that this or super keyword can’t
* be refers in static methods and static initialize blocks.
* @author W3spoint
*/
class Show {
int num = 10;
}
class Display extends Show {
int num = 20;
public static void show(){
//error because super can't be refers in static
//method or static initializer block.
System.out.println("num of super class = " + super.num);
}
public static void display(int num){
//error because this can't be refers in static
//method or static initializer block.
this.num = num;
}
}
public class StaticExample7 {
public static void main(String args[]){
Display obj = new Display();
obj.show();
obj.display(20);
}
} |
/**
* This program is used to show that this or super keyword can’t
* be refers in static methods and static initialize blocks.
* @author W3spoint
*/
class Show {
int num = 10;
} class Display extends Show {
int num = 20;
public static void show(){
//error because super can't be refers in static
//method or static initializer block.
System.out.println("num of super class = " + super.num);
}
public static void display(int num){
//error because this can't be refers in static
//method or static initializer block.
this.num = num;
}
} public class StaticExample7 {
public static void main(String args[]){
Display obj = new Display();
obj.show();
obj.display(20);
}
}
Output:
Exception in thread "main" java.lang.Error:
Unresolved compilation problem:
Cannot use super in a static context
at com.w3spoint.business.Display.show
(StaticExample7.java:18)
at com.w3spoint.business.StaticExample7.main
(StaticExample7.java:32) |
Exception in thread "main" java.lang.Error:
Unresolved compilation problem:
Cannot use super in a static context
at com.w3spoint.business.Display.show
(StaticExample7.java:18)
at com.w3spoint.business.StaticExample7.main
(StaticExample7.java:32)
Download this example.
Next Topic: final in java with example.
Previous Topic: super in java with example.