0

Is there a way to get values of local variables in one class and show that in jsp page?

For example,

public class a { 
    public method b() { 
         String c;

I want to use the String c and show it in jsp page.

${c} 

1) Is there a way to use a local variable of a class from outside of that class?
2) if not, just make the variable global, and use it? ( What I mean by global is using static. I looked it up and Java doesn't have global variable)
3) if so, in JSP page, can you tell me some ways to show that value?

${class.variable name} <--- Would this work as well?

Jin Lee
  • 3,194
  • 12
  • 46
  • 86
  • 1
    1) No. A **local** variable is not visible from outside the block where it is defined. 2) + 3) Theoretically, [yes](https://stackoverflow.com/questions/3732608/how-to-reference-constants-in-el). Anyway, I would not recommend this unless you really know what you are doing, which seems - no offence - not to be your case. – Jozef Chocholacek Jan 29 '19 at 08:45
  • 1
    @JozefChocholacek okay~ thank you for your advice! Maybe, you can put this as answer, so I can accept it. – Jin Lee Jan 29 '19 at 23:56
  • Why using a global variable ? You can just use a class member to hold `c` and use a getter... or simply return `c` from the method. – AxelH Jan 30 '19 at 08:31
  • @AxelH I was just curious. :) I want to know many ways if exists. – Jin Lee Jan 30 '19 at 08:34
  • @AxelH When you return C from the method, I just call that method from other class? ( after importing the class of the method) – Jin Lee Jan 30 '19 at 08:37

1 Answers1

1

1) No. A local variable is not visible from outside the block where it is defined.

2) + 3) Theoretically, yes. Anyway, I would not recommend this unless you really know what you are doing. Solutions like the one in the link should be very exceptional, because they go against the best practices.

Jozef Chocholacek
  • 2,874
  • 2
  • 20
  • 25