3
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!

2 Answers 2

5

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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 -

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.