-3

I get the mentioned error while trying to write to a variable according to the following code given below.

I know I can solve this by declaring the variable (mPager) globally and by making it final. But I don't want to make it final and I want to know why such an error occurs and is there any other fix rather than declaring it globally or by final?

public class fieldsActivity extends Activity {

Button addSiteButton;
Button cancelButton;
Button signInButton;


/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // to create a custom title bar for activity window
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.fields);
    // use custom layout title bar
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.topbar);

    Pager adapter = new Pager();
    ViewPager mPager = (ViewPager) findViewById(R.id.fieldspager);  //Variable of CONCERN
    mPager.setAdapter(adapter);
    mPager.setCurrentItem(1);



    addSiteButton = (Button) findViewById(R.id.addSiteButton);
    addSiteButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
           mPager.setCurrentItem(2, true); //Compilation error happens here.
        }


    });
Anil P Babu
  • 1,235
  • 3
  • 26
  • 47
  • 1
    make pager as `final` – M D Jul 22 '16 at 05:55
  • Yes error can't be more clearer, make your mPager as final. – Pradeep Simha Jul 22 '16 at 05:56
  • http://stackoverflow.com/questions/24001752/why-the-compiler-ask-me-declare-a-variable-final-when-i-use-the-method-onclic – Nabin Jul 22 '16 at 05:58
  • 2
    This links might be helpful http://stackoverflow.com/questions/4732544/why-are-only-final-variables-accessible-in-anonymous-class – Dhruvi Jul 22 '16 at 05:58
  • https://tech-read.com/2008/06/19/why-inner-class-can-access-only-final-variable/ – Dhruvi Jul 22 '16 at 06:00
  • Golden rule of Android development is to know when to use Global variables and when to use local variables. Don't try too hard to optimize the code where it is really difficult to do so. Follow standard development techniques. And for your current problem, making the `mPager` global is best solution. – Mohammed Atif Jul 22 '16 at 06:15
  • up until java 8, it was necessary to make local variables final for them to be accesses inside an anonymous class. In java 8, they introduced a pseudo final like concept which says that if the object is assigned only once, its effectively final. Too bad that android doesn't support all java 8 features. – Kushan Jul 22 '16 at 06:21

1 Answers1

1

It is the standard way java(in java 8 it might be different) deals with closure. As far as I know, when having inner classes the compiler makes a copy of the variable, not the variable itself, via the autogenerated constructor. Then, in order not to have inconsistencies in and outside of the inner class, the variable must be final, otherwise one part can have an outdated status of the variable. Unless you make it global, ofc.

Relevant post:
Why are only final variables accessible in anonymous class?

Community
  • 1
  • 1
Andrei T
  • 2,985
  • 3
  • 21
  • 28
  • it is different in java 8 :) they have introduced effectively final concept. see http://stackoverflow.com/questions/20938095/difference-between-final-and-effectively-final – Kushan Jul 22 '16 at 06:22
  • I read on the way to work, that java 8 is different, did not really see it on my small screen properly, though. thanks for the update anyway. – Andrei T Jul 22 '16 at 06:33
  • @Andrei "in order not to have inconsistencies in and outside of the inner class, the variable must be final," I understood this problem. But how does making the variable global, solves this problem? – Anil P Babu Jul 22 '16 at 06:45
  • @anil_pulikoden this is a bit different, you can start here to understand the problem: http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class. First read to understand clearly what is an static (inner) classes and see how variables behave, etc. After that you will understand why. – Andrei T Jul 22 '16 at 08:44