I learn Java by autodidact..
Here the code:
public class CubicToy { private double side; /* Creates a new instance of CubicToy */ public CubicToy(double side) { this.side = side; } static void test (){ CubicToy m = new CubicToy(3.5); System.out.println(m); System.out.println("This cubic has circumference for each side of "+m.getCircumference(3.5)); System.out.println("This cubic has Area for all side of "+m.getAllArea(3.5)); System.out.println("This cubic has Volume of "+m.getVolume(side)); // here the 1st error } public String toString (){ String str = "This cubic has "+ side + " side lenght"; return str; } public double getCircumference(double side){ return side * 4; } public double getAllArea (double side){ return side * side *6; } public double getVolume (double side){ return side*side*side; } public static void main(String args[]){ test(); // here 2nd error } }
if I compile codes above, I get error(1st error) non-static variable side cannot be referenced from a static context
so I try to change the function of
static void test()into
void test()..
and the result if I compile become (2nd error): non-static method test() cannot be referenced from a static context
next again I try to change
public static void main(String args[])into
public void main(String args[]).. and the result of compile is SUCCESS .. BUT if I run/debug it.. pop-up a message dialog which it's said that Class "CubicToy" does not have a main method.
can someone explain to me why it can work like that?
I have knew that this error can be solveed if I change
m.getVolume(side)into
m.getVolume(3.5)..but I just don't understand why I can not use
m.getVolume(side)
Thanks in advanced for the explaination..