tags:

views:

724

answers:

5

Dear all,,

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..

+5  A: 

Java doesn't have global variables, so there isn't a direct equivalent. However, there's a static keyword in Java that shares the state of a field with all instances of a class, which is a good approximation to what you're describing.

I want to do recursion in java, performing same function that a static keyword in C does...

However, if you're looking to do recursion, are you sure that static variables are what you need? Any special state needed for a recursive function call is almost always passed back to itself, not maintained separately.

John Feminella
What John mentioned seems absolutely true, I doubt the use of static variable in recursive function. It would be clean if you can write the recursive function as well ...
FL4SOF
Thnx a lot for ur answer sir...
AGeek
No problem. If you have another question about recursive functions, do go ahead and ask that one too.
John Feminella
+1  A: 

The concept of static in Java doesn't adhere with the concept of static in C. However, there is a static keyword in Java as well. But its more like a static in C++ then C, with some differences.

Adeel Ansari
+9  A: 

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 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.

Adam Rosenfield
Thnx a lot sir..
AGeek
+4  A: 

The "static" keyword in C actually serves two functions depending on where it's used. Those functions are visibility and duration.

  • When used at file level, it marks an item (variable or function) as non-exported so that a linker cannot see it. This is static as in visibility, duration is the same as the program (i.e., until the program exits). This is useful for encapsulating the item within a single compilation unit (a source file, in its simplest definition). The item is available to the whole compilation unit (assuming it's declared before use).
  • When used within a function, it controls duration (visibility is limited to within the function). In this case, the item is also created once and endures until the program exits. Non-static variables within a function are created and destroyed on function entry and exit.

I gather what you're after is the first type, basically a global variable since I can't immediately see much of a use for the other variant in recursion..

It can't be done since, in Java, everything must belong to a class. The workaround is to create a class holding the "globals" and either:

  • pass that object around so you can reference its members; or
  • construct a singleton item so you can access its members.
paxdiablo
Thnx a lot sir..
AGeek
A: 

You can simulate a static class in java as follows:

   /**
     * Utility class: this class contains only static methods and behaves as a static class.
     */

public abstract class Utilities
    {
        // prevent inheritance
        private Utilities()
        {
        }
    // ... all your static methods here
        public static Person convert(string) {...}
    }

This class cannot be inherited (like final because although abstract it has a private constuctor), cannot be instantiated (like static because abstract) so only static methods in it can be called.

Gerard