java - How do I refrence a non- static method from a static context -


i have constructor function when compile main method non-static method area cannot referenced static context. know simple fix cant quite there.

public class rect{      private double x, y, width, height;       public rect (double x1, double newy, double newwidth, double newheight){        x = x1;        y = newy;        width = newwidth;        height = newheight;      }     public double area(){        return (double) (height * width); } 

and main method

public class testrect{      public static void main(string[] args){         double x = double.parsedouble(args[0]);         double y = double.parsedouble(args[1]);         double height = double.parsedouble(args[2]);         double width = double.parsedouble(args[3]);         rect rect = new rect (x, y, height, width);         double area = rect.area();           }     } 

you need call method on instance of class.

this code:

rect rect = new rect (x, y, height, width); double area = rect.area(); 

should be:

rect rect = new rect (x, y, height, width); double area = rect.area();               ^ check here                 use rect variable, not rect class                 java case sensitive 

Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -