How to reference "global" variable in java function
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
However, the body of the function is defined locally in any given program. What I need to do in one particular program is reference a string defined in the main program, e.g.:
String localVar = globalVar;
where globalVar is defined outside the scope of reportDetail().
Is there a way to do this? (this is jsp BTW)
To be specific, I need a way of getting the UserAgent string: (request.getHeader( "User-Agent" ), in a function without passing it as a function parameter.
The method is static! Just add the user agent to its class as a static variable.
Code:
class ReportDetail implements IReportDetail
{
@Override
public static void reportDetail(Statement stmt, BufferedWriter outFile, String detailColumn) throws Exception
{
System.out.println(userAgent);
}
public static string userAgent;
public class Main
{
public static void main(String[] args)
{
ReportDetail.userAgent = request.getHeader("User-Agent");
ReportDetail.reportDetail(/* parameters go here */);
}
}
IReportDetail cannot be resolved to a type
455:
456: //HttpSession sessionObject = session;
457:
458: class ReportDetail implements IReportDetail
459: {
460: @Override
461: public static void reportDetail(Statement stmt, BufferedWriter outFile, String detailColumn) throws Exception
An error occurred at line: [461] in the jsp file: [/printAPchecks.jsp]
The method reportDetail cannot be declared static; static methods can only be declared in a static or top level type
458: class ReportDetail implements IReportDetail
459: {
460: @Override
461: public static void reportDetail(Statement stmt, BufferedWriter outFile, String detailColumn) throws Exception
462: {
463: ResultSet rs = null;
464: SimpleDateFormat mmddyyyy = new SimpleDateFormat("MM/dd/yyyy");
An error occurred at line: [461] in the jsp file: [/printAPchecks.jsp]
The method reportDetail(Statement, BufferedWriter, String) of type printAPchecks_jsp.ReportDetail must override or implement a supertype method
458: class ReportDetail implements IReportDetail
459: {
460: @Override
461: public static void reportDetail(Statement stmt, BufferedWriter outFile, String detailColumn) throws Exception
462: {
463: ResultSet rs = null;
464: SimpleDateFormat mmddyyyy = new SimpleDateFormat("MM/dd/yyyy");
An error occurred at line: [133] in the jsp file: [/include/pdfMerge.inc]
The method reportDetail(Statement, BufferedWriter, String) is undefined for the type printAPchecks_jsp
130:
131: // Optionally call reportDetail() function to create detail lines on the page
132:
133: if (stmt != null) reportDetail(stmt,outFile,rs.getString(detailColumn)); // note, need maxDetailLines/page
134:
135: outFile.write("<div style=\"page-break-before:always\"></div>\n");
136: }
Is your example missing a '}'? I've added one after "public static string userAgent;".
This whole thing may not work with jsp per the error at 461. I don't believe JSP has the ability to create classes outside of main.
Might well be. I wrote it to get the architectural idea across, not to be compileable (and I did not try to compile it).
Note that "IReportDetail" was a placeholder for the name of the superclass that contains the reportDetail method. You did not name it, and it wasn't in your code sample, so I could not use its actual name in my reply. If you're pasting "IReportDetail" into your actual code, you've misunderstood the recommendation.
Well, as mentioned, I'm using JSP which creates the main class as part of its compilation of the jsp. The generated .java code has for the main class:
Code:
public final class printAPchecks_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent,
org.apache.jasper.runtime.JspSourceImports { ...}
which is likely the equivalent of your 'public class Main{}'. All jsp code is actually expanded within this printAPchecks_jsp class and you cannot define classes external to this class, which is probably what is needed in your example.
In the generated .java code the reportDetail() is simply a function, not a class:
Here is a complete (probably compilable) .jsp program. Unless escaped with some form of "<%", lines in the code are assumed to be HTML statements, like the 1st 3 lines in the example. Java code is bracket by <% someJavaCode %>. The <%@ ... %> delimiters are for including files or imports. The <%! ... %> delimiters are for function definitions. In this example, the main logic calls the pdfmergeDoc() function which, in-turn, calls reportDetail().
Okay, this demo does work. It prints "the user agent":
Code:
class GlobalData
{
static String userAgent;
}
final class printAPChecks_jsp
{
static void reportDetail()
{
System.out.println(GlobalData.userAgent);
}
}
public class GlobalDataDemo
{
public static void main(String[] args)
{
GlobalData.userAgent = "The user agent";
printAPChecks_jsp.reportDetail();
}
};
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.