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!

non-static method getPerimeter() cannot be referenced from a static context

807598Oct 30 2006 — edited Oct 30 2006
I am getting this error message. I assume it is because getArea( ) and getPerimeter( ) are nonstatic and main is static? Can somebody help me? Thanks in advance!

Error messages:
non-static method getArea() cannot be referenced from a static context
System.out.println("\nArea: " + onePlace.format(getArea()));

non-static method getPerimeter() cannot be referenced from a static context
System.out.println("Perimeter: " + onePlace.format(getPerimeter()));
import java.util.Scanner;
import java.text.DecimalFormat;

public class Rectangle
{
	public double length;
	public double width;

	public Rectangle()
	{
		length = 0;
		width = 0;
	}

	public double getLength()
	{
		return length;
	}

	public double getWidth()
	{
		return width;
	}

	public void setLength(double length)
	{
		this.length = length;
	}

	public void setWidth(double width)
	{
		this.width = width;
	}

	public double getArea()
	{
		return (length * width);
	}

/*	public double area(Rectangle R)
	{
		double area = length * width;

		return area;
	}
*/

	public double getPerimeter()
	{
		return ((length * 2) + (width * 2));
	}

/*	public double perimeter(Rectangle R)
	{
		double perimeter = (length * 2) + (width * 2);

		return perimeter;
	}
*/
/*	public String toString()
	{
		double area = 0;
		double perimeter = 0;

		return "Area: " + area + "\tPerimeter: " + perimeter;
	}
*/
	public static void main(String [] args)
	{
		DecimalFormat onePlace = new DecimalFormat("#0.0");
		Rectangle R = new Rectangle();
		Rectangle L = new Rectangle();
		Rectangle W = new Rectangle();

		Scanner input = new Scanner(System.in);

		System.out.println("Enter length of rectangle: ");
		L.setLength(input.nextDouble());

		System.out.println("\nEnter width of rectangle: ");
		W.setWidth(input.nextDouble());

		System.out.println("\nArea: " + onePlace.format(getArea()));
		System.out.println("Perimeter: " + onePlace.format(getPerimeter()));

//		System.out.println(R.toString());
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 27 2006
Added on Oct 30 2006
4 comments
486 views