Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent of C's "static" keyword in Java?

Tags:

java

c

static

I want to know what could be the equivalent keyword in java which could perform same function as "Static keyword in C".. I want to do recursion in java, performing same function that a static keyword in C does...

Please help..

like image 869
AGeek Avatar asked Apr 08 '09 04:04

AGeek


People also ask

Is static in C same in Java?

The static data members are basically same in Java and C++. The static data members are the property of the class, and it is shared to all of the objects.

What is static key C?

Static is a keyword used in C programming language. It can be used with both variables and functions, i.e., we can declare a static variable and static function as well. An ordinary variable is limited to the scope in which it is defined, while the scope of the static variable is throughout the program.

What is static method in C Java?

A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.

What is static key in Java?

The static keyword in Java is mainly used for memory management. The static keyword in Java is used to share the same variable or method of a given class. The users can apply static keywords with variables, methods, blocks, and nested classes. The static keyword belongs to the class than an instance of the class.


1 Answers

C has two entirely different uses of the static keyword, and C++ adds a third use:

// Use 1: declare a variable or function to be local to a given module
// At global scope:
static int global_var;
static void func();

In this case, the global variable global_var and the function void func() can only be accessed inside the file in which they are declared; they cannot be accessed by any other file.

// Use 2: declare a variable inside a function with global scope
void func(void)
{
    static int x;
}

In this case, the variable x is effectively a global variable, in that there is only one instance of it -- multiple calls to func() (including recursive calls) will always access the same variable.

// Use 3 (C++ only): declare a global variable with class scope
class Widget
{
public:
    static int var;
};

In this case, this declares the variable Widget::var as a global variable, but its scope is different. Outside of class member functions, it has to be named as Widget::var; inside class member functions, it can be named as just var. It can also be made protected or private to limit its scope even more.

Now, what are the analogs of these 3 uses in Java?

Case 1 has no direct analog; the closest is declaring objects with package scope, which is done by omitting a public, protected, or private:

class Widget  // Declare a class with package scope
{
    int x;  // Declare a member variable with package scope
    void func() {}  // Declare a member function with package scope
}

In this case, the declared objects are only accessible by classes within the same package; they are not accessible to other packages.

Case 2 also does not have an analog in Java. The closest you can get is by declaring a global variable (that is, a static class variable, since Java doesn't have true global variables in the strictest sense):

class Widget
{
    private static int func_x;
    public static void func()
    {
        // use func_x here in place of 'static int x' in the C example
    }
}

Case 3 is the only case that has a direct analog in Java. In this case, the static keyword serves exactly the same purpose.

like image 61
Adam Rosenfield Avatar answered Sep 18 '22 06:09

Adam Rosenfield