非静态调用 - IntelliJ认为它是从静态方法中获取的

时间:2017-11-26 03:21:39

标签: java object intellij-idea static non-static

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

我有一个对象GeoLocation,我试图在我的代码中使用该对象的非静态方法(distanceFrom())。可以理解的是,当我尝试从Non-static variable cannot be referenced from a static context调用它时,我得到psvm。因此,使用来自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点,我在非静态类中调用distanceFrom(),但在psvm之外。在第2点,我用非静态方法调用它。但在这两种情况下,我仍然收到错误消息。为什么intelliJ似乎在想我的整个java文件是静态的?

这是对象类:

// 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;
    }
}

1 个答案:

答案 0 :(得分:2)

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