Java 无法从静态上下文引用非静态变量/方法!问题是什么?

Java 无法从静态上下文引用非静态变量/方法!问题是什么?,java,object,methods,constructor,static,Java,Object,Methods,Constructor,Static,这个代码有什么问题。。。我正在创建对象。。。那就好了!!是吗?你应该为作业创建一个新类。在这里使用main方法有点混乱。也就是说,问题在于main方法的第一行: import java.util.Scanner; import java.lang.Math; public class Assignment1{ //instance variables double a; double b; double c; // method

这个代码有什么问题。。。我正在创建对象。。。那就好了!!是吗?

你应该为
作业创建一个新类。在这里使用
main
方法有点混乱。也就是说,问题在于
main
方法的第一行:

import java.util.Scanner;
import java.lang.Math;

public class Assignment1{
         //instance variables
         double a;
     double b;
     double c;
    // method
    public double hypot(double x, double y){
        x = x*x;
        y = y*y;
        double z = x+y;
        z = Math.sqrt(z);
        return z;

    }
    // constructor
    public Assignment1(double a , double b, double c){
            this.a = a;
        this.b = b;
        this.c = c;
    }

    public static void main(String [] args){ // execution starts from here
            Assignment1 obj = new Assignment1(a,b,c);
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the values to compute the pythagoras    theorem");

        obj.a = in.nextDouble();
        obj.b = in.nextDouble();
        obj.c = hypot(a,b);
        System.out.println("The hypot is"+ c);


    }


}
访问变量
a
b
c
,它们是非静态变量。这意味着它们不存在于
static main
函数的上下文中

另一种编写方法是:

Assignment1 obj = new Assignment1(a,b,c);

一般的经验法则是,尝试将主类中除了
main
方法之外的所有内容都保留为空。如果调用包含
main
方法
public class main
的类,则更为清晰。希望这有帮助

在您的
main
方法(即
static
)中,您试图访问
a
b
c
字段,这些字段是非静态的,没有任何对
赋值1
实例的引用

您需要了解类的非静态成员属于类的实例,并且只能通过此类实例访问它们

在非静态方法中,例如

// Place this in a file called Assignment.java
import java.util.Scanner;
import java.lang.Math;

public class Assignment {

  double a;
  double b;
  double c;

  public double hypot(double x, double y) {
    x = x*x;
    y = y*y;
    double z = x+y;
    z = Math.sqrt(z);
    return z;
  }

  public Assignment(double a , double b, double c) {
    this.a = a;
    this.b = b;
    this.c = c;
  }

} // end class Assignment

// Place this in a file called Main.java
public class Main {
  public static void main(String [] args){ // execution starts from here

    // Give some values to a, b, and c!
    double a = 1;
    double b = 1;
    double c = 1;

    Assignment obj = new Assignment(a,b,c);
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the values to compute the pythagoras    theorem");

    obj.a = in.nextDouble();
    obj.b = in.nextDouble();
    obj.c = hypot(a,b);
    System.out.println("The hypot is"+ c);
  } // end method main
} // end class Main
您可以在不引用实例的情况下使用
字段
,因为编译器会将
这个。
添加到表示当前实例的变量中。在静态方法中没有
这个
,因为静态方法/字段属于整个类,而不是实例

为解决您的问题,请考虑首先创建“分配1”类的“未设置”实例(假设您将其值设置为<代码> 0代码>代码),如

然后您可以将其字段设置为来自用户的数据,如

Assignment1 obj = new Assignment1(0, 0, 0);
最后,您可以使用

obj.a = in.nextDouble();
obj.b = in.nextDouble();

BTW直接处理对象字段考虑了设计不当。相反,您应该创建getter和setter(像
getA()
setA(intnewa)
)这样的方法),它们将返回或操作它们的值。

a
b
c
实例成员。您的
main
方法是类的成员

你想把这两者混在一起,这是行不通的

另外,由于您的
Assignment1
类实际上不需要自定义构造函数,因此可以删除它

相反,请尝试以下方法:

obj.c = obj.hypot(obj.a, obj.b);
当然错了,, 首先使用以下代码创建Assignment1的实例: 转让1 obj=新转让1(a、b、c)

a、b和c在哪里

也许您应该先使用null: Assignment1 obj=新Assignment1(null,null,null)


然后使用Scanner.in初始化a、b和c

对我来说就像是一个编程任务。。。也许你正试图在一个静态方法中使用一个非静态字段?使用一个。根据OOPS原则,有什么不对?如何交替编码?我添加了另一种编写代码的方法!我保持a、b、c为静态,现在工作正常……:)
obj.c = obj.hypot(obj.a, obj.b);
public static void main(String [] args){ // execution starts from here
        Assignment1 obj = new Assignment1();
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the values to compute the pythagoras    theorem");

        obj.a = in.nextDouble();
        obj.b = in.nextDouble();
        obj.c = hypot(a,b);
        System.out.println("The hypot is"+ c);
 }