-1
import java.util.*;

public class HelloWorld{

   public static void main(String []args){
       static String[] str={"one","two"};
       Date date =new Date();
       Calendar cal = Calendar.getInstance();
       String year = "2018";
       String month ="4";
       int day =10;

       cal.set(Integer.parseInt(year),Integer.parseInt(month),day);
       System.out.println(cal.get(Calendar.DAY_OF_WEEK));
       // etc.
    }
}

Error message :

error: illegal start of expression

static String[] str={"one","two"};

Bentaye
  • 9,403
  • 5
  • 32
  • 45
mohit
  • 11
  • 5
  • 5
    you can't use `static` in the method ! did you mean `final` – Youcef LAIDANI May 08 '18 at 11:53
  • No, I didn't mean final... – mohit May 08 '18 at 11:56
  • A local variable only exist in the scope of the method itself. Having a `static` local variable would not be accessible from `HelloWorld.str` since this is not in the scope of the method. – AxelH May 08 '18 at 11:59
  • https://stackoverflow.com/questions/24269308/why-cant-we-declare-static-variables-inside-a-function-body-even-when-the-functi https://www.quora.com/Why-cant-I-declare-a-static-variable-inside-main-method-in-Java – guleryuz May 08 '18 at 11:59
  • 1
    The real answer here is: you do not understand what the *static* keyword is about. – GhostCat May 08 '18 at 12:01
  • Such use of `static` would be valid in C. It isn’t in Java. It’s a design decision. You may suggest that it be made possible in Java 12; I wouldn’t expect you to get anywhere with it, though. – Ole V.V. May 08 '18 at 12:27

4 Answers4

6

That is invalid syntax. You don't apply the static modifier to local variables. Remove static.

If str is meant to be a class variable, then declare it outside the method:

public class HelloWorld{
    static String[] str={"one","two"};

Static is a concept for members of types (interfaces, classes, enums...). It allows you to reference the variable using just the class name, in this case (HelloWorld.str). When the variable belongs to the local scope, as in the method body, it cannot be declared as static because it is not a member of the class.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • I know its invalid but why?? – mohit May 08 '18 at 11:56
  • 1
    @mohit Why would it be valid ? how would you use it ? – Bentaye May 08 '18 at 11:57
  • `static` is used to access a variable without an instance, a local variable only exist in the scope of the method itself. So having a `static` local variable would not be accessible `HelloWorld.str` since this is not in the scope of the method. – AxelH May 08 '18 at 11:57
  • @mohit `static` *roughly* means "for the class, not for the instance of the class". This definition does not apply to the method/local scope of the variable either way. – ernest_k May 08 '18 at 11:59
0

A class can have static members, but a method cannot have static variables in it.

You could move your static String[] str={"one","two"}; above your declartion for main(..) which would move it into the class's scope.

Dragonthoughts
  • 2,180
  • 8
  • 25
  • 28
0

There are several kinds of variables:

  • Member variables in a class—these are called fields.
  • Variables in a method or block of code—these are called local variables.
  • Variables in method declarations—these are called parameters.

local variables can not be static, only fields can be static. static fields don't belong to any instance, they belong to the class.

In this case, you can set String[] str as a class member:

static String[] str={"one","two"};

public static void main(String []args){
    // use str
} 
xingbin
  • 27,410
  • 9
  • 53
  • 103
-1

Static members are part of the class.. you cannot declare a static variable inside a method.

It should rather be like this

Class Helloworld{ static String [] a={"a","b"};

public static void main(String args){

}

Sribalaji
  • 11
  • 3