Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Static Problem

807599Feb 13 2007 — edited Feb 13 2007
Hopefully (Ha!) a nice and easy problem basically im trying to develop a simple application. The problem im getting is the "Cannot access non static method from a static context"

Unfortunately afaik the main method (ie public static void main(String[] arguments)) HAS to be static! this calls a method within the same class which then calls another method in a different class. The final method called CANNOT be static any suggestions?

Main Class Code:
public class Main {

public static void main(String[] arguments) {

	int Option;
	Option = Integer.parseInt( arguments[0] );	

	if (Option == 1) {
	Main.newcity();
	}	

	}

	public static void newcity() {
		String Name = "Lincoln";
		String Lat = "10";
		String Long = "11";

		new City(Name, Lat, Long);
		City.displaycity();
	}

}
City Class Code
public class City {
	private String Name;
	private String Latitude;
	private String Longitude;

	/**
	 * Constructor for the City.
	 */

	public City(String cityName, String cityLat, String cityLong) {

		Name = cityName;
		Latitude = cityLat;
		Longitude = cityLong;		
	}

	/**
	 * Display the current City.
	 */

public void displaycity() {
		System.out.println("Name: " + Name);
		System.out.println("Latitude: " + Latitude);
		System.out.println("Longitude: " + Longitude);
	}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 13 2007
Added on Feb 13 2007
5 comments
142 views