When I tried to build this program there’s always a error like “non-static method referenced from a static context”
I think that because I can use “addto” function in “main”. So how can I solve this problem? I need a public arraylist because I have to do the calculations in “addto”
Thx!
public class Calculation {
ArrayList<int[]> cal = new ArrayList<>();
public static void main(String[] args) {
System.out.println(addto(3,5));
}
String addto(int figone, int figtwo){
........do the calculations by using arraylist cal
}
}
Really simple?
or
(You could also add a
staticmodifier to theaddtomethod declaration, but you would then need to makecalstatic too so theaddtocan use it. Bad idea.)OK. So what the compilation method is actually saying is that
addtois declared as an instance method … but you are trying to call it without saying which instance to use. In fact, you are trying to call it as if it was a static method.The “fix” (see above) is to create an instance and call the method on that.