1//Java Program to get the cube of a given number using the static method
2using static before a method or variable we can access it by not creating a
3instance of it.in the program we directly used student.cube(5)
4class Calculate{
5 static int cube(int x){
6 return x*x*x;
7 }
8
9 public static void main(String args[]){
10 int result=Calculate.cube(5);
11 System.out.println(result);
12 }
13}
1In Java, static keyword is mainly used for memory management.
2It can be used with variables, methods, blocks and nested classes.
3It is a keyword which is used to share the same variable or method of a given
4class. Basically, static is used for a constant variable or a method
5that is same for every instance of a class
1The static keyword in Java is used for memory management mainly. We can apply static keyword with
2variables, methods, blocks and nested classes. The static keyword belongs to the class
3 than an instance of the class.
4
5The static can be:
6
7Variable (also known as a class variable)
8Method (also known as a class method)
9Block
10Nested class
1static keyword is a non-access modifier. static keyword can be used with
2class level variable, block, method and inner class or nested class.
1In the Java programming language, the keyword static indicates that the particular member belongs to a type itself,
2rather than to an instance of that type.
3This means that only one instance of that static member is created which is shared across all instances of the class.
1We can declare a method as static by adding keyword “static” before method name.
2Let’s see example on static method in java.
3
4public class StaticMethodExample
5{
6 static void print()
7 {
8 System.out.println("in static method.");
9 }
10 public static void main(String[] args)
11 {
12 StaticMethodExample.print();
13 }
14}