检查java中是否已经定义了一个变量 [英] Check if a variable is already defined in java
问题描述
到达那里时,我需要检查变量是否已经在代码中的其他地方定义了.
I need to check if a variable is already defined somewhere else in the code when getting there.
由于JSP中包含了某些内容,因此有时会在两个地方定义一个变量,然后Java返回一个错误.使用jsp:include似乎也不起作用.
Because of includes in JSP, I sometimes get a variable defined in two spots, and java then returns an error. Using the jsp:include doesn't seem to work either.
我正在寻找c的#ifndef
形式的内容,而不是在预处理器中(因为这是java),以避免重复的局部变量问题.
I am looking for something in the form of an #ifndef
of c, but not in the pre-processor (as this is java) to avoid having a duplicate local variable issue.
有时我不能使用==null
或isnull()
,根本就不会定义变量,而不仅仅是未初始化
I cannot use ==null
or isnull()
as sometimes, the variable won't be defined at all, not just uninitialized
推荐答案
我不认为有什么魔术可以解决您的问题,唯一可以做的就是在jsp中应用命名约定文件,尤其是那些打算包含在内的文件.例如,您可以在变量名称前添加前缀,假设您的文件为myfolder/foo.jsp
,变量为bar
,则可以将其重命名为myfolder_foo_jsp_bar
,这样就不会再有任何命名冲突了.
I don't believe that there is a magic trick that will solve your issue, the only thing that could be done is to apply a naming convention in your jsp files especially those which are meant to be included. You can for example add a prefix to the name of your variables, assuming that your file is myfolder/foo.jsp
and your variable is bar
, you can rename it to myfolder_foo_jsp_bar
this way you won't have any naming collision anymore.
这篇关于检查java中是否已经定义了一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!