I heard that static methods should use only static variables in java. But, main method is also static, right?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
First of all, a technicality: it's NOT true that "
main
method is also static". You can define a non-staticmain
method, with whatever signature you choose; it just won't be a valid Java application entry point.With regards to "static methods should use only static variables", this is also NOT true. The key concept here is that static methods and fields are class-specific, not instance-specific. You simply can't access an instance variable/method if you don't actually have an instance of that class; it's a compilation error.
So to be precise, without an instance, you can't access instance fields/methods. You can access static fields/methods without an instance. If you need to access instance fields/methods from a static method, you have to get an instance of that class one way or another, either by simply instantiating it, or by getting a reference to it from a static field or method parameter.
Let's take a look at this simple example:
length
is NOT a static field; it's an instance field of array instances, whichargs
is. The static methodmain
is able to get this instance (and thus access its instance methods and fields) because it's passed in as an argument.Also,
println
is NOT a static method; it's an instance method ofPrintStream
instances. The static methodmain
is able to get this instance by accessing the static fieldout
of the classSystem
.To summarize:
main
public
andstatic
void
and takes aString[]
argument as parametermain
doesn't have to be a Java application entry pointFurthermore,
new
instancestatic
field of a classstatic
method of a classThrowable
Maybe this piece of code will enlighten you:
So you cannot access instance members without an instance. Within the context of a static method you don't have a reference to an instance unless you receive or create one.
Hope this helps.
The static method can't access non-static variables outsides because you can use static method without class initialization, but it doesn't work for non-static outside variable. But you can define and use non-static variable in the static method.
Yes static method cannot call non-static methods or variables of the class directly.
EDIT : One can create any local variables.
To access non-static fields (instance variables) you need to have an instance.
Inside a non-static method,
this
is used as default instance:Inside a static method there is no
this
to use as default instance, but you can1 still access instance variables if you provide the instance:1 - the field must also be accessible by Java's access control (declared
public
or in the same class ...)Your question: is the statement " static methods should use only static variables" correct?
No. The statement is not correct.
The correct statement will be "static methods can only use those instance variables that are defined static"
Take a look at following code and read the comments: