0

What's the purpose of static (static methods, static class, etc)...?

17th Apr 2017, 6:05 AM
Daniel Joris
Daniel Joris - avatar
4 Respostas
+ 1
Static variable is variable that is global to all class instances. static method in class can be called without instantiating an object and has access only to static variables. Static class can be inner class and I think it behaves just like any other outer class you make.
17th Apr 2017, 7:00 AM
Ladislav Milunović
Ladislav Milunović - avatar
17th Apr 2017, 7:28 AM
Meharban Singh
Meharban Singh - avatar
+ 3
Typical usage of static methods and variables: - creating Singleton design pattern - creating utility methods - declaring constants
17th Apr 2017, 7:29 AM
Tamás Barta
Tamás Barta - avatar
+ 1
static methods/variables are sometimes also called class methods/variables in contrast to (normal) instance methods/variables as they are only depending on the class, not a concrete instance. you don't need to create an instance with new to access them. therefore they are easier to test, but you will not use all the aspects of oop. a lot of global available methods/constants are defined that way, e.g. in Java's math package.
17th Apr 2017, 7:13 AM
Volker Milbrandt
Volker Milbrandt - avatar