从静态java方法引用实例变量

从静态java方法引用实例变量,java,reference,static,Java,Reference,Static,问题:在第11行中,编译错误表示,不能从静态上下文引用非静态变量c。为什么?这里“c”是一个实例变量。但是下面的代码是可以的。为什么? 如果我从第8行换到第12行 Line 1: public class C { Line 2: public static void main(String[] args) { Line 3: // method2(); Line 4: } Line 5: Line 6: Line 7: Circl

问题:在第11行中,编译错误表示,不能从静态上下文引用非静态变量c。为什么?这里“c”是一个实例变量。但是下面的代码是可以的。为什么?

如果我从第8行换到第12行

Line 1:  public class C { 
Line 2:      public static void main(String[] args) { 
Line 3:         // method2();
Line 4:      }
Line 5: 
Line 6:      
Line 7:      Circle c = new Circle();
Line 8:      public static void method2() { 
Line 9:          // Circle c = new Circle();
Line 10: 
Line 11:        System.out.println("What is radius "+ c.getRadius()); //compile error : non-static variable c cannot be referenced from a static context.  Why? here "c" is a instance variable. 
Line 12:     }
Line 13: 
Line 14:    
Line 15:  }
Line 16:  
Line 17:   class Circle
Line 18:  {
Line 19:    public  int getRadius()
Line 20:    {
Line 21:        return 3;
Line 22:    }
Line 23:  }
或:


静态
方法内部和方法外部创建实例是不同的

当您在静态方法中访问它们时,变量、引用必须是静态的,或者它们必须是该静态方法的局部变量

案例1

定义
Circle c=new Circle()时在方法外部,它将成为实例成员,而您不能在静态方法中访问它

案例2:

Circle c = new Circle();
public void method2() { 
    System.out.println("What is radius "+ c.getRadius());
}
在本例中,
c
是该方法的本地成员,实例成员永远不会出现在图片中。所有内容都是该方法的本地成员


寓意:您不能在静态方法中访问实例成员。

静态成员可以在没有类实例的情况下使用,但您的c变量只有在类实例化后才开始存在。编译器将检查代码是否可以运行,并且在实例化之前,可以调用静态方法,但不能调用该c实例

第二个块是ok,因为method2是一个实例方法,在第一次尝试中,method2仍然是静态的


其他解决方案包括:将c设置为静态变量,或将其设置为method2中的局部变量。

当您使用
static
关键字创建方法时,它是静态方法或类方法。这意味着对象的每个实例都是相同的,因此不能从对象内部访问实例变量。从静态方法内部,您只能访问静态变量或调用类的静态方法。

从:

基于上述情况:

  • 您的原始代码有一个试图访问实例的类方法 不允许的变量
  • 在更改代码的第一个示例中,您声明了变量
    c
    静态方法中,以便可以访问变量 通过这种方法
  • 在第二个更改的示例中,该方法是一个实例方法, 尝试访问实例变量,这也是允许的

在第一个示例中,
圆圈c
是一个成员变量。 在创建类
C
的实例时,可以创建该类的实例。 当您在static方法中调用该方法时,没有
圆圈c
的实例

您的第二个示例之所以有效,是因为您在方法中创建了实例并在其中调用它

您的第三个示例之所以有效,是因为它是一个成员variabel和一个成员方法,所以java可以在调用
method2()
之前确保它是
Circle c
的一个实例


我希望我能帮上忙。

在OP中,对静态的含义似乎有些混淆

静态函数是在没有任何封闭实例的情况下可用的函数。这意味着可以根据语法Class.function调用它。非静态函数要求您创建对象的实例

Not all combinations of instance and class variables and methods are allowed:

- Instance methods can access instance variables and instance methods
  directly.
- Instance methods can access class variables and class methods
  directly.
- Class methods can access class variables and class methods directly.
- Class methods cannot access instance variables or instance methods
  directly—they must use an object reference. Also, class methods
  cannot use the this keyword as there is no instance for this to refer to.
为了做到这一点,静态函数应该是无状态的是一个必要条件。也就是说,访问的唯一变量要么是函数的作用域,要么是方法参数,要么是封闭类的静态成员。因此,禁止静态方法尝试更改其封闭类的任何非静态成员

因此:

所以我可以从下面的代码中调用这些函数@

public class Demo{
    public static int staticCounter = 0;
    public int instanceCounter = 0;

    public static int getStaticCounter(){
        return staticCounter++;
    }

    public int getInstanceCounter(){
        staticCounter++;
        return instanceCounter++;
    }
}

您正在使用实例方法,因此需要该类的对象来访问它。只有当您有该类的对象时,才能访问实例方法(如果方法或变量中没有静态关键字,则为实例方法或变量)
但对于静态方法,您不需要创建该类的对象。

请在将来对代码进行格式化时投入更多精力。使用空格而不是制表符,并进行编辑,直到预览显示您在阅读问题时希望它的样子
不是声明为静态的,而是作为
C
的实例变量,您需要使声明为静态的,即
static Circle C=new Circle()
C
的封闭实例,该实例必须声明为
static
。代码格式化的问题在哪里?具体点。是“成员变量”吗?变量是成员。我想你是说“实例变量”吧?他可能通过实例调用该方法,因此您的第二点也不正确。但他不会每次都调用一个实例。
Not all combinations of instance and class variables and methods are allowed:

- Instance methods can access instance variables and instance methods
  directly.
- Instance methods can access class variables and class methods
  directly.
- Class methods can access class variables and class methods directly.
- Class methods cannot access instance variables or instance methods
  directly—they must use an object reference. Also, class methods
  cannot use the this keyword as there is no instance for this to refer to.
Object instance = using new Object{}
instance.method()
public class Demo{
    public static int staticCounter = 0;
    public int instanceCounter = 0;

    public static int getStaticCounter(){
        return staticCounter++;
    }

    public int getInstanceCounter(){
        staticCounter++;
        return instanceCounter++;
    }
}
public class widget{
    public static void main(String arg[]){
        System.out.println(demo.getStaticCounter()); // returns zero 
        //Node that I can access the static method before creating any object;
        Demo widget = new Demo();
        System.out.println(widget.getInstanceCounter()); //returns zero
        System.out.println(widget.getInstanceCounter()); //returns one


        System.out.println(widget.getStaticCounter()); // returns 2.
        // note that its generally considered bad practice to call a static method via an instance. Call Demo.getStaticCOunter() instead.

        Demo anotherWidget = new Demo();
        System.out.println(anotherWidget.getInstanceCounter()); //returns zero
        System.out.println(anotherWidget.getInstanceCounter()); //returns one
        //since these get a new copy of the instance variable...
        System.out.println(Demo.getStaticCounter()); // returns 5.
        //but share their static variable.
    }
}