I have read this question, but my issue is different because mine isn't about this error message specifically; I was just using it to realize I had made a different kind of mistake. Please read my answer to my question - if you still feel like this is a duplicate, feel free to mark it as such.
I have an object, GeoLocation
, and I'm trying to use a non-static method (distanceFrom()
) from that object in my code. Understandably, I got a Non-static variable cannot be referenced from a static context
when trying to call it from a psvm
. So, using advice from this page, I tried moving the call to various other parts of my code. However, I keep getting the same message, even if I'm not making the call from a static context. Here's my code:
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();*/
}
}
At point 1 I'm calling distanceFrom()
inside the non-static class, but outside of psvm
. At point 2, I'm calling it in a non-static method. But in both cases, I still got the error message. Why is intelliJ seemingly thinking that my whole java file is static?
Here's the object 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;
}
}