Static method in java

I heard that static methods should only use static variables in java. But, the main method is also static, isn't it?

+4
source share
8 answers

Your question: is the statement that "static methods should use only static variables" correctly?

Not. The statement is incorrect.

The correct statement would be "static methods can only use instance variables that are defined by static"

Take a look at the following code and read the comments:

Class A{ int i; static int j; public static void methodA(){ i = 5; //cannot use since i is not static j = 2; //can use. int k = 3; //k is local variable and can be used no problem **EDIT:**//if you want to access i A a = new A(); //Ai = 5; //can use. ai = 5; // it should be non-capital "a" right? } } 
+12
source

First of all, technicality: It is NOT true that the main method is also static. "You can define a non-static main method with any signature you choose; it simply will not be a valid entry point for a Java application.

As for the "static methods that should only use static variables", this is also NOT true. The key concept here is that static methods and fields are class specific and not specific instances. You simply cannot access the instance of the variable / method unless you actually have an instance of this class; This is a compilation error.

To be precise, without an instance, you cannot access the fields / methods of the instance. You can access static fields / methods without an instance. If you need to access field instances / methods from a static method, you must get an instance of this class one way or another, either simply by creating an instance of it, or by getting a link to it from a static field or method parameter.

Let's look at this simple example:

 public static void main(String args[]) { System.out.println(args.length); } 

length NOT a static field; This is the instance field of the array instances, which args is. The static main method is able to get this instance (and thus access its methods and instance fields) because it is passed as an argument.

In addition, println NOT a static method; This is an instance method of PrintStream instances. The static main method can get this instance by accessing the static out field of the System class.


Summarizing:

  • A Java application entry point is a method that:
    • called main
    • - public and static
    • returns void and accepts String[] as a parameter
  • The method named main should not be a Java application entry point
    • (but it’s best to reserve this name for this purpose)

Moreover,

  • Instance fields / methods can only be accessed through an instance
  • Static fields / methods can be accessed without an instance.
  • Static methods can get an instance of a class in one of the following ways:
    • instantiation new
    • passing it as an argument
    • access to it through the static field of the class
    • accepts it as the return value of the static method of the class
    • catch it like thrown Throwable
+16
source

Perhaps this piece of code will enlighten you:

 public class Main { private String instanceField = "Joe"; private void instanceMethod() { System.out.println("Instance name=" + instanceField); } public static void main(String[] args) { // cannot change instance field without an instance instanceField = "Indy"; // compilation error // cannot call an instance method without an instance instanceMethod(); // compilation error // create an instance Main instance = new Main(); // change instance field instance.instanceField = "Sydney"; // call instance method instance.instanceMethod(); } } 

Therefore, you cannot access instance members without an instance. In the context of a static method, you do not have a reference to an instance if you have not received it or created it.

Hope this helps.

+2
source

The static method is called on the class instance, not on the class object. This means that the static method cannot access instance variables, since they are only created in the object.

If you want to access an instance variable with a static method, you must declare this variable as static.

 public class Test { private static int value = 0; public static void main(String[] args) { value++; } } 

But honestly, this is not a good idea to write everything in static methods. It is better to use the main method to create new objects.

+1
source

To access non-static fields (instance variables), you need to have an instance.
Inside the non-static method, this used as the default instance:

 class AnyClass { private String nonStaticField = "Non static"; void nonStaticMethod() { this.nonStaticField = "a text"; // accessing field of this nonStaticField = "a text"; // same as before } } 

Inside the static method, there is no this for use as the default instance, but you can 1 still access instance instances if you provide an instance:

 class AnyClass { private String nonStaticField = "Non static"; static void staticMethod() { AnyClass example = new AnyClass(); example.nonStaticField = "new value for non-static field"; } } 

1 - the field must also be accessible using Java access control (declared public or in the same class ...)

+1
source

The important thing is that if you do not create an instance of the class, the instance variables do not exist, which means that the JVM does not know that there is such a variable as int i if you do not create an instance for this class. therefore, using an instance variable in a static method is a compilation error.

 class A{ int i; static int j; static int b(){ i=10; // cannot be found A a= new A(); ai=10;//can be found in a instance } } 

But we can use instance variables in instance methods because instance methods are only called when an object is created, so there is no problem using instance variables inside it.

Hope this is clear!

+1
source

Yes, a static method cannot directly call non-static methods or class variables.

EDIT . You can create any local variables.

0
source

The static method cannot access non-stationary variables, because you can use the static method without initializing the class, but it does not work for a non-static external variable. But you can define and use non-static variables in a static method.

0
source

Source: https://habr.com/ru/post/1303323/


All Articles