6

Can we declare Static Variables inside Main method? Because I am getting an error message:

Illegal Start of Expression
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Priti
  • 97
  • 1
  • 1
  • 2
  • 2
    Please paste the relevant code. – Jeff Swensen Jul 31 '10 at 15:41
  • 2
    If, as some commenters suggest, your question is inspired by the fact that you *can* do this in C, then you should indicate that in your question, so that answers can help you by pointing out the differences between Java and C. – Tyler Jul 31 '10 at 15:48
  • Perhaps we can answer better if we know why you need to use a static variable inside a method? All variables declared inside a method are scoped for that method only. Let us know what problem you are trying to solve and I am sure we can come up with some good answers for you. – CoolBeans Jul 31 '10 at 18:43

6 Answers6

33

Obviously, no, we can't.

In Java, static means that it's a variable/method of a class, it belongs to the whole class but not to one of its certain objects.

This means that static keyword can be used only in a 'class scope' i.e. it doesn't have any sense inside methods.

comatose
  • 1,952
  • 7
  • 26
  • 42
Roman
  • 64,384
  • 92
  • 238
  • 332
  • 2
    It could have the meaning it has in C functions ... From wikipedia: "Static local variables: variables declared as static inside inside a function are statically allocated while having the same scope as automatic local variables. Hence whatever values the function puts into its static local variables during one call will still be present when the function is called again." –  Jul 31 '10 at 15:44
  • 1
    @functional: my suggestion is based on tag `java` near the question. – Roman Jul 31 '10 at 15:45
  • 3
    Ok, got that. I'm just saying that it could make sense to be inside methods too. That's all –  Jul 31 '10 at 15:45
  • Ah, I see. I didn't know you could do that in C. But in Java, you definitely can't. – Tyler Jul 31 '10 at 15:47
  • 1
    @functional - it would only make sense in Java, if you also considered it made sense in C. Personally, I think that that was a flaw in the syntax of C language. It is a kludgy way of doing what Java and C++ do using visibility modifiers. – Stephen C Jul 31 '10 at 16:41
6

You can use static variables inside your main method (or any other method), but you need to declare them in the class:

This is totally fine:

public Class YourClass {
  static int someNumber = 5;

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

This is fine too, but in this case, someNumber is a local variable, not a static one.

public Class YourClass {

  public static void main(String[] args) {
    int someNumber = 5;
    System.out.println(someNumber);
  }
}
Tyler
  • 21,762
  • 11
  • 61
  • 90
6

Because the static variables are allocated memory at the class loading time,and the memory is allocated only once.Now if you have a static variable inside a method,then that variable comes under the method's scope,not class's scope,and JVM is unable to allocate memory to it,because a method is called by the help of class's object,and that is at runtime,not at class loading time.

Shekhar
  • 61
  • 1
  • 1
3

As static variables are available for the entire class so conceptually it can only be declared after the class whose scope is global where as static block or methods all have their own scope.

vivek
  • 31
  • 1
2

In C, you can have statically allocated locally scoped variables. Unfortunately this is not directly supported in Java. But you can achieve the same effect by using nested classes.

For example, the following is allowed but it is bad engineering, because the scope of x is much larger than it needs to be. Also there is a non-obvious dependency between two members (x and getNextValue).

static int x = 42;
public static int getNextValue() {
    return ++x;
}

One would really like to do the following, but it is not legal:

public static int getNextValue() {
    static int x = 42;             // not legal :-(
    return ++x;
}

However you could do this instead,

public static class getNext {
    static int x = 42; 
    public static int value() {
        return ++x;
    }
}

It is better engineering at the expense of some ugliness.

John Henckel
  • 10,274
  • 3
  • 79
  • 79
2

You cannot, why would you want to do that? You can always declare it on the class level where it belongs.

Karel Petranek
  • 15,005
  • 4
  • 44
  • 68
  • 1
    Static variables inside methods are legal in C and C++, perhaps that is his background. – Matt Greer Jul 31 '10 at 15:43
  • I know but Java isn't C++ :) Static variables in C/C++ have the advantage of not being deleted after the function ends so you can pass them anywhere. With Java's garbage collector you can just pass the variable around without any worries that's why I asked what his point was. Thanks for your comment. – Karel Petranek Jul 31 '10 at 15:52