简体   繁体   中英

Static variable in Java compile time error, in C one time memory allocation

When initialising a static variable in C, by functionality, memory allocation takes place only once. Hence on incrementing the variable in the same function, it retains the incremented value during the next function call. However, the same test fails in Java, giving a compilation error, specifying, static cannot be used.

Tried coding in Java and C and different results obtained. C executes, while Java provides a compilation error.

C Code:

#include<stdio.h>
void func()
{
    static int k=0;
    printf("%d ",++k);
}   
int main() 
 {
  int i;
  for(i=0;i<5;i++)
    func();
  return 0;
 }

Java Code:

 public class Test
  {
     public static void main(String[] args)
      {
         Test obj=new Test();
         for(int i=1;i<6;i++)
         obj.increment();
      } 
     void increment()
      {
          static int i=0;
         System.out.println(i++);
      }
 }

C output:

1 2 3 4 5

Java compile time error:

java: illegal start of expression

in Java there is no static local variables - the only places to put static keyword are:

field

public class Test {
    static Object staticField;
    // ...

method

public class Test {
    static Object getStaticField() {
        // ...
    }
    // ...

inner class

public class Test {
    static class InnerTest {
       // ...
    }
    // ...

static initialization block

public class Test {
    static Object staticField;
    static { // this will be called before constructor
        staticField = new Object();
    // ...

import

import static java.lang.Math.PI; // now you can use PI without 'Math.'

Java and C are separate and very different languages, syntactic similarities notwithstanding. The static keyword has completely different meaning in Java from the two different meanings it has in different C contexts.

In C, static is either about linkage (related Java concept: visibility) or about storage duration (your case; no direct Java analog). In Java, on the other hand, static is about whether a variable or method is associated with class instances or only with an overall class. Java allows that to be specified only for class members, not for local variables. These really aren't comparable.

static variable in c is much different than the static variable in java. -Static variables in c 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,so as your answer is 1,2,3...... -but in java.. When a variable is declared as static, then a single copy of variable is created and shared among all objects at (class level). Static variables are, essentially, global variables. All instances of the class share the same static variable. NOTE:in java you can only declare static variable in class level.

public class Test
{
    static int i=0;
    public static void main(String[] args)
    {
         Test obj=new Test();
         for(int i=1;i<6;i++)
         obj.increment();
    } 
    void increment()
   {
       System.out.println(i++);
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM