开发者

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

about "static function" in c++,what is correspo开发者_Go百科nding 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"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜