1

I have this code:

public void FbShare(JSONObject parameters) {

    if (Session.getActiveSession() == null || !Session.getActiveSession().isOpened()) {

        Session.StatusCallback callback = new Session.StatusCallback() {

            @Override
            public void call(Session session, SessionState state, Exception exception) {

                if (state.isOpened()) {
                    publishFeedDialog(parameters);  // <--- HERE
                }
            }
        };

        Session.openActiveSession(this, true, callback);
    } else {
        publishFeedDialog(parameters);
    }
}

Why is parameters not accessible in the publishFeedDialog(parameters)call?

1
  • 1
    make it final -> public void FbShare(final JSONObject parameters) Commented Oct 15, 2014 at 14:10

5 Answers 5

4

Set the parameters parameter as final:

public void FbShare(final JSONObject parameters) {
 //...    
}

For an overview of what's happening, see:

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

Comments

4

Trying making that argument final:

public void FbShare(final JSONObject parameters) {

4 Comments

final in java is the same as const in C++? Does it mean that I cannot change the values of parameters argument?
No, not the same. final says the reference won't change. Java is pass by value, and for objects the value is a reference type. You can't change the reference, but you can change the mutable state of the object the reference points to if it allows you to. const in C++ is a much wider ranging keyword.
You mean the reference cannot point to other object, or the object it is pointing will not be changes inside the function.
I think I was clear: the reference itself cannot change.
2

Change the first line to

public void FbShare(final JSONObject parameters) {

Comments

1

The compiler error message tells you everything you need to know, if you can decipher it:

 Cannot refer to the non-final local variable parameters in an enclosing scope

That is, the variable parameters in the enclosing scope is not final, so it can't be referred to.

To fix that, make it final:

 public void FbShare(final JSONObject parameters) {

Java isn't clever enough to keep track of what parameters refers to, unless you use final to guarantee that it's always going to point at the same object.

This would equally apply if you were referring to a local variable that wasn't a method parameter:

 public void foo() {
      final String bar = ...;
      Callback callback = new Callback() {
           void invoke() { 
               something(bar); 
           }
      };

Comments

0

Make your parameters as final so it can be used within your anonymous class.

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.