-5

I am learning both Java and C sort of concurrently and when attempting to connect some common features of these two languages, I sometimes get a little bit confused. I try to identify the difference in "static" usage when in Java and in C and hope someone could point out whether my ways of understanding are correct. And is there any of my logic could be improved(Also want to know how you distinguish the usage of "static" in these two languages)?

So in C, "static" is used when you hope that some certain variables are only initialized once. And I think that's all.

For example:

int main(void){
    for(int i=0;i<=5;i++){
        static int x=35; //x is only initialized once
        x++;
        printf("%d ",x); //"36 37 38 39 40 41 "will be printed out 
    }
    x++; //This is not allowed as x is not a global variable.
    return 0;
}

But in Java, "static" is used when you hope some certain variables or methods can be accessed or called with the name of the class that holds the variables or methods, rather than some objects. And I think (usually, if not always?) you do "ClassName.SomeVariables" or ClassName.SomeMethods" in the driver class?

Therefore, is the usage of "static" in C and Java is very different? And a relevant question: how can the "static" in C be achieved by and features in Java?

Thanks in advance. ^_^

Danny.C
  • 5
  • 3

1 Answers1

1

Static Variables in C

Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.

Static variable remains in memory while the program is running. A normal or auto variable is destroyed when a function call where the variable was declared is over.

for example it is used to track the number of times the function is call.

For example below program prints “1 2”

#include<stdio.h>
int fun()
{
  static int count = 0;
  count++;
  return count;
}

int main()
{
  printf("%d ", fun());
  printf("%d ", fun());
  return 0;
}

But below program prints "1 1"

#include<stdio.h>
int fun()
{
  int count = 0;
  count++;
  return count;
}

int main()
{
  printf("%d ", fun());
  printf("%d ", fun());
  return 0;
}

Static Variables in Java

In java ,static variable is one that's associated with a class, not objects of that class.

Let's take a look at an example. To keep things as simple as possible, we'll use a public static variable. If you haven't come across public yet, don't worry about it! In these particular examples, actually it makes no difference.

public class Stuff {
    public static String name = "I'm a static variable";
}

Now we can access this class in another class ,

public class Application {

    public static void main(String[] args) {
        System.out.println(Stuff.name);
    }

}

You can see that we can happily access the 'name' instance variable in the Stuff class without actually creating an object of type Stuff.

another example which is used to track the number of objects of the existing class,

class VariableDemo
{
   static int count=0;
   public void increment()
   {
       count++;
   }
   public static void main(String args[])
   {
       VariableDemo obj1=new VariableDemo();
       VariableDemo obj2=new VariableDemo();
       obj1.increment();
       obj2.increment();
       System.out.println("Obj1: count is="+obj1.count);
       System.out.println("Obj2: count is="+obj2.count);
   }
}

output :

Obj1: count is=2
Obj2: count is=2
Usman
  • 2,322
  • 14
  • 26