Static keyword is used for almost same purpose in both C++ and Java. There are some differences though. This post covers similarities and differences of static keyword in C++ and Java.
Static Data Members: Like C++, static data members in Java are class members and shared among all objects. For example, in the following Java PRogram, static variable count is used to count the number of objects created.
Static Member Methods: Like C++, methods declared as static are class members and have following restrictions:
1) They can only call other static methods. For example, the following program fails in compilation. fun() is non-static and it is called in static main()
3) They cannot access this or super . For example, the following program fails in compilation.
Static Local Variables: Unlike C++, Java doesn’t support static local variables. For example, the following Java program fails in compilation.
新闻热点
疑难解答
图片精选