» » Static methods in java example

  • A method which has static keyword in its definition is called static method.
  1. static void print(){
  2.  
  3. }

  • 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:


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  
  5. static void show(){
  6. System.out.println("static method"):
  7. }
  8.  
  9. public static void main(String[] args)
  10. {
  11.  
  12. show();
  13.  
  14. }
  15. }
Output:

  1. static method

Static method called from variable assignment:


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  static int x=show();
  5. static int show(){
  6. System.out.println("static method called"):
  7. return 10;
  8. }
  9.  
  10. public static void main(String[] args)
  11. {
  12.  
  13. System.our.println(x);
  14.  
  15. }
  16. }
Output:

  1. static method called
  2. 10

Static method called from static block:


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  static{
  5.  show();
  6. }

  7. static void show(){
  8. System.out.println("static method called"):
  9. }
  10.  
  11. }
Output:

  1. 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:


  1. package com.instanceofjava;
  2. class StaticMehodDemo
  3. {
  4.  
  5. static void show(){
  6.  
  7. System.out.println("show method called");
  8.  
  9. }
  10.  
  11. static void print(){
  12.  
  13. System.out.println("print method called");
  14.  
  15. }
  16.  
  17. static void display(){
  18.  
  19. System.out.println("display method called");
  20.  
  21. }
  22.  
  23.  
  24. public static void main(String[] args)
  25. {
  26.  
  27. System.our.println("main method called");
  28.  
  29. show();
  30. print();
  31. display();
  32.  
  33.  
  34. }
  35. }


Output:

  1. main method called
  2. print method called
  3. show method called
  4. 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.
  1. int x=20;
  2. x=x; // valid statement
Example program:



  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  static int a=10;

  5. public static void main(String[] args){
  6.   
  7. int a=20;
  8. a=a;
  9. System.out.println("a="+a);
  10.  System.out.println("StaticDemo.a="+StaticDemo.a);
  11.  
  12. }
  13.  
  14. }
Output:

  1. 20
  2. 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:


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  static int x=10;
  5.  
  6. static void m1(int x){ 
  7.  
  8.  System.out.println(x);
  9.  System.out.println(StaticDemo.x);

  10. }

  11. public static void main(String[] args){
  12.   
  13.  m1(37);
  14.  System.out.println(x);
  15.  
  16. }
  17.  
  18. }
Output:

  1. 37
  2. 10

 

You might Like:

 

 

 

 


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu