0

I'm trying to get a variable from another function in the same .java file.

This is an example:

public static void showWindow(User user)
{
    String checkList = null;

    if (user.getNetConnection().getBonus() >= 0.)
    {
        checkList = "variable1";
    }
    else
    {
        checkList = "variable2";
    }
    showCommunity(user, checkList);
}

private static void loadSingle()
{
    allSingle = new LinkedList<>();
    try (Connection con = DatabaseFactory.getInstance().getConnection();
        PreparedStatement statement = con.prepareStatement(checkList);
        ResultSet rset = statement.executeQuery())...(the rest of funcion here)

There is anyway to use the variable checklist from function showWindow inside the function loadSingle?

5
  • With php I can do it easy, but I'm newbie in java! Sorry if this is a so easy question! Commented Aug 7, 2015 at 18:21
  • 1
    Since it's a static method,if checkList is public you could access it by ClassName.checkList (although getters/setters are recommended) so its like ClassName.getCheckList(); considering they are in different class. If its the same class, just checkList or getCheckList(). Commented Aug 7, 2015 at 18:21
  • Yes, just use it. Since the local variable there is called checkBuffList and not checkList it must be a static variable in class scope, so you can access it in any method. Commented Aug 7, 2015 at 18:22
  • Sorry, inside the method, I did wrong with checkBuffList, error is only in the example... sorry for bad example! But I believe with the reply "Declare it outside of the method, this will work. I'm so noob in java! Commented Aug 7, 2015 at 18:40
  • in my answer there's a code example of declaring it outside Commented Aug 7, 2015 at 18:42

2 Answers 2

4

Declare it outside of the method (I'm assuming you meant checkBuffList).

Sign up to request clarification or add additional context in comments.

3 Comments

Too bad OP fixed it :D.
I have fixed it, sorry for the "checkBuffList"!
This is only one file of that I need to change, I'll learn more about java. I'm using PHP for a few years, but using with procedure and never with oriented object. I don't know why, oriented object does'n enter in my mind. Everyone says "Oriented object is good and easy" but I did so many things to try understand and when I start to understand I always think "Procedure is more easy" and back to procedure again! I need to stop it and learn oriented object!
1

Not sure if what you made was just a typo or what, but you are defining a variable checkBuffList which you are never using.

If it is in fact a typo, what you should be able to do is defining a static variable and a getter, like this

private static String checkList;

public static String getCheckList(){
    return checkList;
}

So if you want to call it from the same class you can simply use checkList, but if you need it from an outside class simply call ClassName.getCheckList().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.