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}
1Static keyword is used a lot in java.
2 Static means, you
3can access those static variables
4without creating an object,
5just by using a class name.
6This means that only one instance of
7that static member is created which
8is shared across all instances of the class.
9Basically we use static keyword when
10all members share same instance.
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.
1static keyword: belongs to the class, also can be called through the class
2static variable: declared outside any block with static keyword
3static: runs first, only runs one time
4static member:
51. static variables
62. static methods
73. static initializer block
84. static inner class(nested class)
9static methods: methods that we can call it through the class name.
10 belongs to the class
11Ex: Webdriver driver = WebdriverFactory.getDriver();
12 ********* Static methods only accepts class member(static) ************
13********* None static can ONLY be called through the object ***********
14
15Static only accept static. Anything not static you cannot call directly.
16You can call Instance variable in Constructor.
17You cannot call instance variable in static you have to create object first.
18You can call non static (instance variable) in the instance block.
19You can call static in the instance block
20Static variable da last initialization will be final value.
21
22