The Artima Developer Community
Sponsored Link

Java Buzz Forum
Static methods in java example

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Static methods in java example Posted: Apr 20, 2015 3:56 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Static methods in java example
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

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

 



Read: Static methods in java example

Topic: Failing fast for the uptight Previous Topic   Next Topic Topic: Ecosystems: how do they work? - Software Defined Talk #030

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use