Variable Is Accessed Within Inner Class. Needs to Be Declared Final

Variable is accessed within inner class. Needs to be declared final.But I don't want to declared Final

Answering your literal question:

An inner class doesn't really get access to the outside variables (like your flag), Java only creates the illusion of access.

Technically, you get a field named flag (automatically created invisibly by the compiler) with a copy of the flag variable's content from the moment you instantiate the inner class. It's similar to passing flag as an additional argument into the constructor and storing it in a local field of the same name.

But then, to not destroy the illusion of access to your flag variable, Java forbids you to change flag, as technically only the local field could be changed, not the outside variable.

So, a solution for that part (already mentioned by others) is to have a boolean[] flagHolder = new boolean[1]; and use flag[0] for communication.

But that won't help you in your use case.

When the onResponse() method is called (when receiving your response), your https() method has most probably already returned, so it won't be able to see anything that might result from the later response.

Some formal remark:

Please, don't show code as screenshots (it's against the stackoverflow rules), but copy it into your question as text. That makes it easier for us to make our own tests and improvements...

Variable is accessed from within inner class needs to be declared final

You can just go ahead and make fileName final. I know you think that this will cause problems, but it won't.

the "fileName" variable needs to be put in final, due to Java giving the pointer of the object and not the value itself. But if I do so, only the last fileName selected will be sent for any button.

On each iteration through the loop, you're declaring a different String instance. Each one will be final, but they will not be "shared" across loop iterations.

I made a tiny app that iterated over an array of Strings, doing something very similar to your code. Despite declaring my String as final, each button still toasts a different name when I click it.

for (int i = 0; i < names.length; i++) {
final String name = names[i];

Button button = new Button(this);
button.setText(name);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
}
});

root.addView(button);
}

variable is accessed from within inner class needs to be declared final - Error

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?

Variable is accessed from within inner class, needs to be declared final error in custom adapter

You can get the necessary information from the parameters of the OnCheckedChangeListener: buttonView is the checked element. So all you need to do is the following:

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
CheckBox checkbox;
if (buttonView instanceof CheckBox )
{
checkbox = (CheckBox)buttonView;
}
else return;

if (isChecked) {
selected.add(checkbox.getText().toString());
}else{
selected.remove(checkbox.getText().toString());
}
}

Java local variable accessed from within inner class; needs to be declared final, why it works in NetBeans?

Prior to Java 8, all local variables that need to be accessed in an inner class need to be declared final. This is due to an implementation detail -- local variables are implemented as hidden instance variables of the inner class. If the variable could be changed, then the copy in the inner class would be incorrect.

In Java 8, this requirement was relaxed to "final" or "effectively final" (scroll down about 2/3 of the way).

However, starting in Java SE 8, a local class can access local variables and parameters of the enclosing block that are final or effectively final. A variable or parameter whose value is never changed after it is initialized is effectively final.

The variable doesn't have to be declared explicitly to be final, it just has to be effectively final -- not explicitly final, just not changed.

You are not changing Button, so it's effectively final and it compiles under Java 8. However, Java 7 still required it to be declared explicitly final so there is a compiler error.

Fix your path so that javac is your Java 8 installation. It's possible you have a JRE (no "javac" executable) that is Java 8 but a JDK 1.7 (Java 7) installation, which could explain your versioning output. In that case you would need to download and install the Java 8 JDK (1.8).



Related Topics



Leave a reply



Submit