-2

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

Rachel
  • 100,387
  • 116
  • 269
  • 365
  • This is a partial duplicate with http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-java – mjv Oct 24 '09 at 04:49
  • 5
    Because even a minor amount of effort searching would have turned up the answer in seconds. – Azeem.Butt Oct 24 '09 at 04:56
  • 4
    And in turn, you should trust http://java.sun.com (for Java questions) more than Stack Overflow. When something in the documentation is hard to understand, then come to Stack Overflow with your question. Otherwise, it looks like you've put in absolutely no effort at all. – Greg Hewgill Oct 24 '09 at 05:04

5 Answers5

3

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.

Nick Jurista
  • 1,423
  • 12
  • 15
2

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
  • 7,515
  • 1
  • 25
  • 41
1

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
  • 951,095
  • 183
  • 1,149
  • 1,285
1

Use static for fields and methods that can only have one instance. That means they are not relevant to instances of a class, but to the class itself. For example the main thread (public static void main).

Fly
  • 21
  • 2
0

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
  • 17,688
  • 15
  • 65
  • 110