0

Since inside static methods we:

  1. Cannot make static reference to a non-static field
  1. Then a local variable in a static method SHOULD be static as well.

But according to this answer here: Are local variables in static methods also static?

  1. Local variables in static methods are just local variables in a static method. They're not static, and they're not special in any way.

There is a conflict between my deduction (2) and the statement taken from Ernests answer (3) which I cannot explain.

Could someone explain if my deduction is wrong, and why is that?
And if I am not wrong what is the correct answer to the question "Are local variables in static methods also static" ?

EDIT: I am NOT confused by what a local variable OR a member variable (field) OR a static is.

I just thought that:
IF "static methods cannot make reference to non static field" AND "fields are variables" THEN "variables in static methods are also static"
making a variable in a static method both local && static

Thank you.

5
  • Why do you think a local variable is static? Commented Nov 18, 2014 at 9:56
  • 1
    Seems you need to examine that Commented Nov 18, 2014 at 9:58
  • @immibis Don't forget that the word field is just a naming convention used to describe member variables. So a field is a variable type, just like a local variable is a variable type. Of course they are not the same type. Commented Nov 18, 2014 at 13:05
  • @Xipo Don't forget that the phrase member variable is just a naming convention used to describe fields (in some languages other than Java). Commented Nov 18, 2014 at 22:36
  • @immibis I agree with you, the only thing I wanted to point out is that fields are variables. Commented Nov 19, 2014 at 8:36

4 Answers 4

5

A field cannot be referred by a static method because it belongs to an Instance of a Class and therefore it is out of the static method's scope.

Local variables are never static. By "local" variables, it is understood variables declared and used in a code block, whether that code block be a static initializer, class initializer or method/constructor body. The scope of such variables is of course determined by the code block they are declared in.

Not to be mixed with variables declared in classes (fields), whether they be instance variables (non static; one per instance) or class variables (static; one per class).

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

Comments

1

A local variable is not a field.

2 Comments

@Xipo You implied it: "Cannot make static reference to a non-static field" >> Then a local variable in a static method SHOULD be static as well. That's what I understood at least.
Ok I get it, I am just saying that I was literal and was not implying anything. I explain myself here.
1
  1. Variables declared inside a method live on the stack. These are called Local variables
  2. Variables declared as static live in the class. These are called Class variables. (though they are often referred to as static variables)
  3. Variables declared nonstatic inside the class live inside the specific instance. These are called Instance variables.

Basically every time you enter a static or nonstatic method, memory is reserved for each variable you declare inside that block. This memory is freed when the block is exited. (exit from a method, or exiting a { } block. The lifespan of local variables is always the block you declare them in.

Comments

0

The simple conflict in your exception is that you expect a local variable to be a field. Which is not true.

A local variable is just living inside a method. Where it doesn't matter if this method is private, public, static, final or anything else.

While a field is living with the Object instance.

And a static field is living with the class.

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.