+ 2

I understand how declaring a variable as static affects its relationship with objects, but I'm having difficulty understanding how doing so with methods has an affect.

If I understand correctly, WITHOUT the 'static' keyword, a variable will store a different value that is unique to the object that invoked its method. On the other hand, variables that ARE declared as 'static' - their values are shared amongst all objects. With regards to static methods, what affect will it have that would otherwise be different if it weren't declared as static?

30th Mar 2016, 12:10 AM
xDD90x
xDD90x - avatar
1 Réponse
+ 1
making a method static allows you to run it without creating an object of that method's class. Methods in the Math class are like that. For example you can call them by invoking the class name rather than from a Math object, like Math.sqrt(2)
3rd May 2016, 8:59 PM
He Lewei
He Lewei - avatar