Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的静态方法_Java_Static - Fatal编程技术网

java中的静态方法

java中的静态方法,java,static,Java,Static,我听说在java中静态方法应该只使用静态变量。 但是,主要的方法也是静态的,对吗 对类实例而不是类的对象调用静态方法。这意味着,静态方法无法访问实例变量,因为它们仅在对象中实例化 如果要使用静态方法访问实例变量,则必须将该变量声明为静态变量 public class Test { private static int value = 0; public static void main(String[] args) { value++; } } 但老实

我听说在java中静态方法应该只使用静态变量。
但是,主要的方法也是静态的,对吗

对类实例而不是类的对象调用静态方法。这意味着,静态方法无法访问实例变量,因为它们仅在对象中实例化

如果要使用静态方法访问实例变量,则必须将该变量声明为静态变量

public class Test {
    private static int value = 0;

    public static void main(String[] args) {
        value++;
    }
}

但老实说,用静态方法编写所有内容并不是最好的主意。最好使用main方法实例化新对象。

是静态方法不能直接调用类的非静态方法或变量


EDIT:可以创建任何局部变量。

首先,一个技术性问题是:“
main
方法也不是静态的”。您可以使用您选择的任何签名定义非静态
main
方法;它不是一个有效的Java应用程序入口点

关于“静态方法应该只使用静态变量”,这也是不正确的。这里的关键概念是静态方法和字段是特定于类的,而不是特定于实例的。如果您实际上没有该类的实例,您就无法访问实例变量/方法;这是一个编译错误

class A{
    int i;
    static int j;
    static int b(){
        i=10; // cannot be found 
        A a= new A();
        a.i=10;//can be found in a's instance
    }
}
因此,准确地说,没有实例,就无法访问实例字段/方法。您可以在没有实例的情况下访问静态字段/方法。如果需要从静态方法访问实例字段/方法,则必须以某种方式获取该类的实例,方法可以是简单地实例化该类,也可以是从静态字段或方法参数获取对该类的引用

让我们看一看这个简单的例子:

public static void main(String args[]) {
  System.out.println(args.length);
}
length
不是一个静态字段;它是数组实例的一个实例字段,即
args
。静态方法
main
能够获取此实例(从而访问其实例方法和字段),因为它是作为参数传入的

而且,
println
不是静态方法;它是
PrintStream
实例的实例方法。静态方法
main
可以通过访问类
System
的静态字段
out
来获取此实例


总结如下:

  • Java应用程序入口点是一种方法,它:
    • 名为
      main
    • 是公共的和静态的
    • 返回
      void
      并将
      字符串[]
      参数作为参数
  • 名为
    main
    的方法不必是Java应用程序入口点
    • (但最好为此保留此名称)
此外,

  • 实例字段/方法只能通过实例访问
  • 可以在没有实例的情况下访问静态字段/方法
  • 静态方法可以通过以下方式之一获取类的实例:
    • 创建新的
      实例
    • 作为论据通过的
    • 通过类的
      静态
      字段访问它
    • 接受它作为类的
      静态
      方法的返回值
    • 将其捕获为抛出的
      Throwable

    • 你的问题是:“静态方法应该只使用静态变量”的说法正确吗

      不,这句话不正确

      正确的语句是“静态方法只能使用静态定义的实例变量”

      查看以下代码并阅读注释:

      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();
              //A.i = 5; //can use.  
              a.i = 5; // it should be non-capital "a" right?
          }
      }
      

      要访问非静态字段(实例变量),您需要有一个实例。
      在非静态方法中,
      用作默认实例:

      class AnyClass  {
      
          private String nonStaticField = "Non static";
      
          void nonStaticMethod() {
              this.nonStaticField = "a text";  // accessing field of this
              nonStaticField = "a text";       // same as before
          }
      }
      
      class AnyClass {
      
          private String nonStaticField = "Non static";
      
          static void staticMethod() {
              AnyClass example = new AnyClass();
              example.nonStaticField = "new value for non-static field";
          }
      }
      

      在静态方法中,没有
      可用作默认实例,但如果提供实例,则仍可1访问实例变量:

      class AnyClass  {
      
          private String nonStaticField = "Non static";
      
          void nonStaticMethod() {
              this.nonStaticField = "a text";  // accessing field of this
              nonStaticField = "a text";       // same as before
          }
      }
      
      class AnyClass {
      
          private String nonStaticField = "Non static";
      
          static void staticMethod() {
              AnyClass example = new AnyClass();
              example.nonStaticField = "new value for non-static field";
          }
      }
      

      1-该字段也必须由Java的访问控制(声明为
      public
      或在同一类中…)访问

      也许这段代码会启发您:

      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();  
          }
      }
      
      因此,您无法在没有实例的情况下访问实例成员。在静态方法的上下文中,除非接收或创建实例,否则没有对实例的引用


      希望这有帮助。

      静态方法不能访问外部的非静态变量,因为您可以在不初始化类的情况下使用静态方法,但它不适用于外部的非静态变量。但是您可以在静态方法中定义和使用非静态变量。

      重要的一点是,除非您创建一个类的实例,否则实例变量实际上不存在,这意味着JVM不存在类似int i的变量,除非您为该类创建一个实例。因此,在静态方法中使用实例变量是一个编译错误

      class A{
          int i;
          static int j;
          static int b(){
              i=10; // cannot be found 
              A a= new A();
              a.i=10;//can be found in a's instance
          }
      }
      
      但是,我们可以在实例方法中使用实例变量,因为只有在创建对象时才会调用实例方法,所以在其中使用实例变量没有问题


      希望你清楚这些事情

      是的,
      main
      方法是
      static
      -您的问题是什么?我们可能不在main中使用静态变量。我只是想确认语句“静态方法应该只使用静态变量”。如果它是正确的,那么v如何在main中使用非stativ变量?如果您尝试在main类中创建一个普通变量。您不能在静态void main函数中访问它。那么我们如何在main方法中使用非静态变量呢?我认为您需要展示一个示例,说明您认为“在main中使用非静态变量”的实际含义。我想您可能会对什么是(非)静态变量感到困惑。我修改了答案以在静态方法中包含“如何访问非静态实例变量”“静态定义的实例变量”是不正确的。如果它们是静态定义的,则变量为name