0
1.3kviews
Static data members and Methods

Mumbai University > Information Technology > Sem 3 > Object Oriented Programming Methodology

Marks: 10M

Year: May 2016

1 Answer
0
3views

Static Method:

  • A method which has static keyword in its definition is called static method.

    static void print(){

    }

  • JVM will not execute static methods by default. They are executed only if they are called explicitly by developer either from main method or from static variable as its assignment statement or from static block.

Static method called from main method:

package com.instanceofjava;
class StaticDemo
{
static void show(){
System.out.println("static method"):
}
 public static void main(String[] args)
{
show();
}
}

Output:

static method

Static method called from variable assignment:
package com.instanceofjava;
class StaticDemo
{
 static int x=show();
static int show(){
System.out.println("static method called"):
return 10;
} 
public static void main(String[] args)
{
System.our.println(x);
}
}

Output:

static method called
10
Static method called from static block:
package com.instanceofjava;
class StaticDemo
{
 static{
 show();
}
static void show(){
System.out.println("static method called"):
}}

Output:

static method called

Order of Execution:

  • Static methods are executed in the order of they are called, not in the order of they are defined.
  • All static methods are executed in java stack area by creating separate stack frame.
  • When a method is called from main method, JVM creates stack frame in main thread for that method execution.
  • The stack frame is destroyed immediately after method execution is completed.

Example program on order of execution of static methods:

package com.instanceofjava;
class StaticMehodDemo
{
static void show(){
System.out.println("show method called");
}
 static void print(){
System.out.println("print method called");
}

static void display(){
System.out.println("display method called");
}
 public static void main(String[] args)
{
System.our.println("main method called");
show();
print();
display();
}
}

Output:

main method called

print method called

show method called

display method called

Variable initialization with same variable:

  • We can initialize variable with same variable name. this assignment is valid. In this case the variable value is replaced with same value.

int x=20;

x=x; // valid statement

Example program:

package com.instanceofjava;
class StaticDemo
{
 static int a=10;
public static void main(String[] args){
int a=20;
a=a;
System.out.println("a="+a);
 System.out.println("StaticDemo.a="+StaticDemo.a);
}
}

Output:

20

10

Local preference with parameters:

  • Parameters are also treated as local variables.
  • Hence if parameter declared with same static variable name and if we want to access static variable in presence of parameter or if we want to initialize static variable with parameter name we must refer variable with class name.

Example program:

package com.instanceofjava;
class StaticDemo
{
 static int x=10;
static void m1(int x){ 
 System.out.println(x);
 System.out.println(StaticDemo.x);
}

public static void main(String[] args){
 m1(37);
 System.out.println(x);
}
}

Output:

37

10

Please log in to add an answer.