Java Static [duplicate]

2019-01-20 06:54发布

Duplicate: What does the 'static' keyword do in a class?


I've read this post already.

What does the "static" keyword in a method do?

I remember being told that static != clingy...but that is pretty much all I know about this keyword.

5条回答
爷、活的狠高调
2楼-- · 2019-01-20 07:34

a static method is one that's established for the class. It doesn't need (and doesn't have) a this pointer, and can't access instance data. So you can write something ike

public class Hello {

    void instanceHello() {
         System,out.println("Hello from the instance.");
    }

    public static void main(int argc, String[] argv){
         // The main method is defined even though there are no instances
         System.out.println("Hello from main.");
         instanceHello();  // but this is a syntax error;
         Hello h = new Hello();
         h.instanceHello();  // this isn't though 
    }
}
查看更多
仙女界的扛把子
3楼-- · 2019-01-20 07:35

The value of a static variable within a method is stored between calls to that method



public void method() {
  static int callCount = 0;
  callCount++;
  System.out.println("Calls: " + callCount);
}

method(); // "Calls: 1"
method(); // "Calls: 2"
method(); // "Calls: 3"

Note that this is completely different from a static method. A static method is called upon the class it is defined in instead of an instance of this class.



class MyClass {
  public static void staticMethod() { ... }
  public void nonStaticMethod();
}

Myclass.staticMethod();

MyClass instance = new MyClass();
instance.nonStaticMethod();

查看更多
祖国的老花朵
4楼-- · 2019-01-20 07:39

A static method belongs to the Class it is defined in and not to instances of objects of that Class, as non-static methods do. As a side-effect of not belonging to the instances of a Class, it's a compile error to try and access non-static fields in a static method. There's no "this" for the static methods to get access of the non-static fields from.

The Java Math class is a great example because it's loaded with static methods. You never create an instance of the Math class, you just call methods directly from the class.

Math.abs(3.14);
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-01-20 07:47

Static variables and methods belong to the class and not instance, although you can refer them from an instance reference. Usually, you use the Class name to access them.

If a method is declared as static, you don't need the object's instance in which it is defined to invoke it. Now, you might want to know when could such a situation arise? Consider the main method of java

public static void main(String[] args)

Why is it declared static? It's because in order to start your program this method should begin execution. And, since the program hasn't initialized there is no way you could create an instance of the class in which it is declared. Therefore, you are required to declare the class as public. And, this static method gets called when the class is loaded in memory through

java YourClassName

Besides this, static methods are used to modify static variables. They cannot manipulate non-static instance variables.

Also, be aware, that static holds a different meaning in another language like C.

查看更多
霸刀☆藐视天下
6楼-- · 2019-01-20 07:48

Static class variables can be thought of as a global class. No matter how many instances of the class you have, there is just one instance of each static variable.

Static methods don't use any non-static class variables and they can be called directly from outside of the class without having to instantiate the class itself.

查看更多
登录 后发表回答