chandler11able
chandler11able

Reputation: 123

Java Class variable Error?

public class class2 {
    static int number3 = 86;

    public static void Whale() {
        static int number4 = 86;

    }

}

why is it i get an error on line number 5 and not line number 2? Thanks!

Upvotes: 3

Views: 76

Answers (2)

namalfernandolk
namalfernandolk

Reputation: 9134

You need to understand that java doesn't support static local variables unlike C/C++. But in some cases Closures may help you for your goal.

Try these some sources for Closures -

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

Because Whale is a method, and you can't define a static field within a method. You can have a local number4 like

public static void Whale() {
    int number4 = 86;

or a static field like

static int number4 = 86;
public static void Whale() {

finally, by convention class names should start with a capital letter (CamelCase) and method names with a lower case letter (camelCase).

Upvotes: 5

Related Questions