0

I am trying to access String operation that is in public String postForm, through a public class, but when in public String Sum I try to call optest.postForm (operation), it shows the error cannot find symbol.

public class op {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String postForm(@QueryParam("operacion") String operacion) {
        return operacion;
    }
}

@GET 
@Path("/calculadora/suma")
public String Suma(@QueryParam("num1") double num1,@QueryParam("num2") double num2)    {
    return Double.toString($Suma(num1,num2));
}

double $Suma(double num1,double num2) {
    op optest = new op();
    optest.postForm(operacion);
    double resultado;
    resultado = num1 + num2;
    return resultado;
}
CannedMoose
  • 509
  • 1
  • 10
  • 18
  • In `Suma` you try to use `operacion` in this: `optest.postForm(operacion);` Where is **that** `operacion` variable declared? – Stephen C Apr 29 '18 at 05:20
  • the operacion variable is declared in public String postForm(@QueryParam("operacion") String operacion) – Ives Rodriguez Apr 29 '18 at 05:20
  • Wrong. That is declaring a local variable. It is not in scope in `$Suma`. – Stephen C Apr 29 '18 at 05:21
  • (And what is with the `$Suma` identifier? The JLS strongly advises against using `$` in identifiers. You are liable to get burned in the future if you do stuff like that.) – Stephen C Apr 29 '18 at 05:24
  • I have closed this as a dup of a question that explains what the compilation error means. Yours is just another example. In this case, the problem is that you are attempting to refer to a variable that is not in scope. I don't know how you would fix it .... because I can't figure out what your code is actually trying to do. – Stephen C Apr 29 '18 at 05:30
  • when I create a REST method, the values that are received from the REST resource must be objects. – Ives Rodriguez Apr 29 '18 at 05:31
  • Yes. But the servlet object that these methods belong to is stateless. You can't use it to hold values from one call to the next one. You need to store them ... somewhere else. And attempting to use a method's local variables to store information beyond the scope of the method call makes no sense. – Stephen C Apr 29 '18 at 05:32
  • How do I store the value? – Ives Rodriguez Apr 29 '18 at 05:37
  • Where do you want to store it? This is the basic problem in a RESTful API you operate on objects, but you don't seem to have any object that represents the "calculador". Clearly, the servlet object itself can't represent it because servlets are stateless. I think you need to look for a tutorial that explains how to represent object state in a RESTful server. – Stephen C Apr 29 '18 at 05:45
  • in a site that the operacion variable can be accessed in other restful methods – Ives Rodriguez Apr 29 '18 at 05:50
  • Well, as I explained, that `operacion` variable can only be accessed inside the `postForm` method. So that approach is not going to work. You need to find a tutorial, I think. – Stephen C Apr 29 '18 at 05:51

0 Answers0