static function javas 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: why we use static in java

In Java, static keyword is mainly used for memory management.
It can be used with variables, methods, blocks and nested classes.
It is a keyword which is used to share the same variable or method of a given
class. Basically, static is used for a constant variable or a method
that is same for every instance of a class

Tags:

Java Example