非静态调用 - IntelliJ 认为它是从静态方法访问的

Non-Static Call - IntelliJ Thinks It's Being Accessed From Static Method

我已阅读 this question,但我的问题有所不同,因为我的问题与此错误消息无关;我只是用它来意识到我犯了另一种错误。请阅读我对问题的回答 - 如果您仍然觉得这是重复的,请随时将其标记为重复。

我有一个对象 GeoLocation,我正在尝试在我的代码中使用该对象的非静态方法 (distanceFrom())。可以理解的是,我在尝试从 psvm 调用它时得到了 Non-static variable cannot be referenced from a static context。因此,根据 this page 的建议,我尝试将调用移至代码的其他各个部分。但是,我不断收到相同的消息,即使我不是从静态上下文进行调用也是如此。这是我的代码:

public class GeoLocationClient {

    /*1 stashStudio = GeoLocation.distanceFrom();*/

    public static void main(String[] args) {
        GeoLocation theStash = new GeoLocation(34.988889, -106.614444);
        System.out.println("the stash is at " + theStash.toString());

        GeoLocation ABQStudio = new GeoLocation(0.0, 0.0);
        System.out.println("ABQ studio is at " + ABQStudio.toString());

        GeoLocation FBIBuilding = new GeoLocation(0.0, 0.0);
        System.out.println("FBI building is at " + FBIBuilding.toString());


        System.out.println("distance in miles between:");
    }

    public void distances(GeoLocation place) {
    /*2    double stashStudio = GeoLocation.distanceFrom();*/
    }
}

在第 1 点,我在非静态 class 内部调用 distanceFrom(),但在 psvm 外部。在第 2 点,我用非静态方法调用它。但是在这两种情况下,我仍然收到错误消息。为什么 intelliJ 似乎认为我的整个 java 文件是静态的?

这是对象 class:

// This class stores information about a location on Earth.  Locations are
// specified using latitude and longitude.  The class includes a method for
// computing the distance between two locations.

public class GeoLocation {
    public static final double RADIUS = 3963.1676;  // Earth radius in miles

    private double latitude;
    private double longitude;

    // constructs a geo location object with given latitude and longitude
    public GeoLocation(double theLatitude, double theLongitude) {
        latitude = theLatitude;
        longitude = theLongitude;
    }

    // returns the latitude of this geo location
    public double getLatitude() {
        return latitude;
    }

    // returns the longitude of this geo location
    public double getLongitude() {
        return longitude;
    }

    // returns a string representation of this geo location
    public String toString() {
        return "latitude: " + latitude + ", longitude: " + longitude;
    }

    // returns the distance in miles between this geo location and the given
    // other geo location
    public double distanceFrom(GeoLocation other) {
        double lat1 = Math.toRadians(latitude);
        double long1 = Math.toRadians(longitude);
        double lat2 = Math.toRadians(other.latitude);
        double long2 = Math.toRadians(other.longitude);
        // apply the spherical law of cosines with a triangle composed of the
        // two locations and the north pole
        double theCos = Math.sin(lat1) * Math.sin(lat2) +
            Math.cos(lat1) * Math.cos(lat2) * Math.cos(long1 - long2);
        double arcLength = Math.acos(theCos);
        return arcLength * RADIUS;
    }
}

您将方法作为 class 方法调用,而您应该在 class 的实例上调用它。而不是 GeoLocation.distanceFrom() 你需要打电话(比如在第 2 点) place.distanceFrom()