+ 2

Please Explain the Use of Static in java briefly with a good example.

It would be nice if 2 example programs with and without static is explained in the answer.

3rd Feb 2017, 6:17 PM
Abhijith R
Abhijith R - avatar
3 Answers
+ 6
class Car { public void any() { // do anything } } class Bus { public static void any() { // do anything } } Car car = new Car(); // create object first (non-static) car.any(); // then call method Bus.any(); // direct call method (static)
6th Feb 2017, 10:44 AM
Adrian M. R.
Adrian M. R. - avatar
+ 2
So that means , if the method is static then there is no need to create an object to call that method ?
6th Feb 2017, 11:19 AM
Abhijith R
Abhijith R - avatar
+ 1
yes
6th Feb 2017, 11:22 AM
Adrian M. R.
Adrian M. R. - avatar