Static method 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: what is static method

A static method belongs to the class rather than the object.
There is no need to create the object to call the static methods.
A static method can access and change the value of the static variable

Example 3: static in java

static keyword is a non-access modifier. static keyword can be used with 
class level variable, block, method and inner class or nested class.

Example 4: do i have to use static methods in java main

Use a static method 
when you want to be able to access the method 
without an instance of the class

Example 5: java what is static

In the Java programming language, the keyword static indicates that the particular member belongs to a type itself,
rather than to an instance of that type.
This means that only one instance of that static member is created which is shared across all instances of the class.

Example 6: static in java

The static keyword in Java is used for memory management mainly. We can apply static keyword with
variables, methods, blocks and nested classes. The static keyword belongs to the class 
  than an instance of the class.

The static can be:

Variable (also known as a class variable)
Method (also known as a class method)
Block
Nested class

Tags:

Java Example