·  JAVA – Syntax – Keywords – static

 

- Info:

  - static keyword defines that property or method can be accessed directly through Class name.

    They can still be accessed through Object but this is not recomended.

  - static property will be comon to all objects, new copy will not be made for each created Object.

 

- Example:

  - Create JAVA Console Application Test.java

 

    Test.java

    public class Test{ 

      public static void main(String[] args) { 

        

        //ACCES STATIC STUFF THROUGH CLASS.

        Monkey.increase();         

        System.out.println("Number of monkeys is " + Monkey.cnt);

        

        //ACCESSING STATIC STUFF THROUGH OBJECT IS NOT RECOMENDED.

        Monkey jo = new Monkey();

               jo.cnt=10;

               jo.increase();

               

      }

    }

   

    class Monkey{

      static int  cnt = 0;              //Number of monkeys.

      static void increase() { cnt++; } //Increase number of monkeys.

    }