Software & Finance





Java Static Variable to Count the Function Calls





 

I have given here Java code to explain how a static variable can be used to count the function call.

 

 

Source Code


import java.lang.*;

import java.util.*;

import java.io.*;

 

 

class FunctionCount

{

   static int count = 0; 

  

   public static void TestFunction()

   {

         System.out.format("Number of times called: %d\n", ++count);

   }

  

   public static void main(String[] args)

   {

         for (int i = 0; i < 5; i++)

            TestFunction();

   }

}

Output


Number of times called: 1

Number of times called: 2

Number of times called: 3

Number of times called: 4

Number of times called: 5

Press any key to continue . . .