+ 1

What is the of use static in java?

Im just confused since there ar e instance where its not used

2nd Jun 2018, 3:19 PM
Russel Dungo
Russel Dungo - avatar
2 odpowiedzi
+ 3
static members are the opposite of instance members - static belongs to the class and not to instances of the class, so all instances share access to that member. You could have a static count variable that increments every time a new object is created, this variable will be accessible from all classes, and provides a single point of truth as to how many objects there are for this class... mostly ;) static methods can be called without an instance, and cannot refer to instance members (because which instance would you use?)
2nd Jun 2018, 3:50 PM
Dan Walker
Dan Walker - avatar
+ 2
Static is basically saying there is only 1 of them...like the MATH class for instance, using a method from the MATH class like .pow means you don't have to initialize that object.
2nd Jun 2018, 4:51 PM
Apple Blossom
Apple Blossom - avatar