views:

353

answers:

4

What is the importance of Static keyword in Java and in C++ and how it's functionality differ in both programming languages ?

A: 

Maybe this link will give you a better idea: http://www.pp.rhul.ac.uk/~george/PH2150/html/node48.html

It has a visual diagram that may make it easier to understand.

wambotron
A: 

It works the same way in both languages. I assume you know what's object-oriented programming, and what's the difference between classes and objects/instances. So, if you mark a method or variable as "static", it operates on a class level, not instance level. All objects/instances share the same value of the "static" variable.

Jaanus
A: 

There are 2 meanings for static. The first if you have a static variable, this means there is only 1 instance of this variable. It works pretty much the same in all programming languages with the keyword.

A static function is a function that can be called, even if the class it resides in is not instantiated. Static functions are necessary in C# and Java because you cant declare functions in these languages which have no encompassing class.

in C++, you can declare functions in the global namespace. In this language, static functions are used to denote that a function belongs to the class, but you dont have to instantiate the class to use the function. You could use a static function to access private variables of the class. Also note that in C++, static functions have a known memory address, so you can use function pointers to point to them without instantiating the class.

Andrew Keith
A: 

For Java, Understanding Instance and Class Members is a good place to start.

For C++, Microsoft has a reference on the static keyword.

There are many readily available programming language resources that will help you understand what the static keyword means. The above are two of them that I found with a quick Google search.

Greg Hewgill