static keyword in java' code example

Example 1: static in java

//Java Program to get the cube of a given number using the static method  
using static before a method or variable we can access it by not creating a 
instance of it.in the program we directly used student.cube(5)
class Calculate{  
  static int cube(int x){  
  return x*x*x;  
  }  
  
  public static void main(String args[]){  
  int result=Calculate.cube(5);  
  System.out.println(result);  
  }  
}

Example 2: static keyword in java

// example on static method in java.
public class StaticMethodExample
{
   static void print()
   {
      System.out.println("in static method.");
   }
   public static void main(String[] args)
   {
      StaticMethodExample.print();
   }
}

Tags:

Java Example