Java static variable, method, block, static inner class, interface static method

preview_player
Показать описание
java static keyword is to create class specific methods and properties. We can use static at following places:

1. static variable - useful to create constants
2. static method - useful to create utility methods
3. static block - useful to initialize static variables and load other resources before instance creation. We can access only class static variables and methods in static block.
4. static inner class - it's almost same as variable, we can create instance just like using static variable.
5. static method in interfaces - this was introduced in Java 8, so make sure you are on Java 8 or higher version otherwise you will get compile time error.

Рекомендации по теме
Комментарии
Автор

Sir, excellent videos you have provided.Very very helpful to us.kindly provide some tricky collection problems.Thanks a lot

happusinha
Автор

Please help, anyone can explain this.

Why "static method called' is printed twice? Not able to understand this.

If I remove below code then also I am getting all the output. What is the point of this then??? -

int x = Test.i;

Test.i = 20;
int y = Test.add(10, 20);


Output after removing above code will be:

inside static block
STATIC METHOD called
60
No Test instance created
INNER INSTANCE Class
INTERFACE static method

gauravpande
Автор

what is significance of static method in interface ? Why it was not allowed in pre java 8 release.

abhishekgupta