简体   繁体   中英

about “static function” in c++,what is corresponding feature in Java

about "static function" in c++,what is corresponding feature in Java

Thank you

You can declare a class method as static

public static int test(int i) {
...
}

You have static functions in Java too.

class MyClass{
    public static int MyFunc() {
           ...
    }
}

The feature in Java corresponding to a C++ static member function is a static method.

Java has no feature directly corresponding to a C++ static free function, since it has no free functions at all and (if I remember rightly) no way of restricting access to a source file.

You can get in the same ballpark with either a private static method (which has "too small" accessibility - just the class), or a package-protected static method ("too big" - the whole package). But the way static non-member functions are used in C++ has a lot to do with the C++ build model, it's a way of defining functions in header files. The same concern doesn't apply to Java.

java has a static keyword for methods. If you want to make a method that cannot be overridden use the keyword "final"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM