The final local variable checkstate cannot be assigned, since it is defined in an enclosing type

The final local variable checkstate cannot be assigned, since it is defined in an enclosing type

By : Grizix
Date : November 19 2020, 03:59 PM
should help you out I don't know want you meant to do with your code. It's a little fuzzy. As the Yazan said it's better to implement OnClickListener interface to avoid sending final to it's method. After that why everything is final? You must define your variables in class level (class variables) to access them from other methods easily and the onCreate() method set their primary values. So whatever I fixed your code like below without any error but I still didn't what It meant to do :
code :
public class TimerActivity extends Activity
    CountDownTimer cntTimer = null;
    ImageButton startTimerButton;
    EditText timerText;
    TextView timerTextValue;
    boolean checkstate =false;
    protected void onCreate(Bundle savedInstanceState)

        startTimerButton = (ImageButton)findViewById(R.id.timer1ImageButton);

        timerText = (EditText) findViewById(R.id.timerEditText1);

        timerTextValue = (TextView) findViewById(R.id.timerTextView);


    public void timerCountDown()
        Integer input = 0;
                 input = Integer.parseInt(timerText.getText().toString())*1000 ;

        CountDownTimer timer = new CountDownTimer(input, 1000)
             public void onTick(long millisUntilFinished) 
                 timerTextValue.setText("seconds remaining: " + millisUntilFinished / 1000);

             public void onFinish() 



    public void timerStatus(final CountDownTimer downTimer) 
        startTimerButton.setOnClickListener(new View.OnClickListener() 
              public void onClick(View v) 
                    checkstate = true;
                    checkstate = false;


Share : facebook icon twitter icon
Issue - The final local variable od cannot be assigned, since it is defined in an enclosing type

Issue - The final local variable od cannot be assigned, since it is defined in an enclosing type

By : Yisheng Zhang
Date : March 29 2020, 07:55 AM
should help you out A variable is available only in the scope it was declared. In your case the scope is inside the run() method. If you want it available outside - it should be declared outside.
code :
 public static void main(String[] args) {
    final MyObj  obj = new MyObj();
    Timer t = new Timer();
    t.schedule(new TimerTask(){
        public void run(){



private static class MyObj {
    private double od;
    public void set(double val) {
        od = val;
    public double get() {
        return od;
The final local variable cannot be assigned, since it is defined in an enclosing type java

The final local variable cannot be assigned, since it is defined in an enclosing type java

By : websurfshop
Date : March 29 2020, 07:55 AM
like below fixes the issue 1) final is mean that you will never change this variable. So you should use global variable or init it whith empty array and then fill it. Anyway, you have strange algorithm...
2) You start Thread. So all code inside it will be asynchronously. And your operation return child; (in your case) always return null.
code :
public interface GetChildList{
        public void onGetChildList(List<String> list);
import com.example.hakslogin.GetChildList;

public class ChildActivity extends ActionBarActivity implements GetChildList {

    Button btn_home;
    Button btn_add;
    private HandleJSON obj;
    public String urlString = "http://192.168.x.xx:xxxx/getdb";

    List<String> child = new ArrayList<String>();
    protected void onCreate(Bundle savedInstanceState) {

        obj = new HandleJSON(urlString);
        lv_child = (ListView)findViewById(R.id.lv_child); 
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, child);
    Handler mHandler = new Handler() {
      public void handleMessage(android.os.Message msg) {
        lv_child = (ListView)findViewById(R.id.lv_child); 
         String arr[]=child.toArray(new String[list.size()]);
         String[] Temp= new String[2];
         Temp[0] = arr[2].toString();
         String s = Temp[0].toString();
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,array);

    public void onGetChildList(List<String> list) {
       //this method will be called after thread in fetchJSONChild ended
       child = list;

public void fetchJSONChild(final GetChildList callBack){

        final List<String> child = new ArrayList<String>();
        Thread thread = new Thread(new Runnable(){

            public void run() {

                try {

                    URL url = new URL("http://192.168.x.xx:xxxx/childform_list/");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setReadTimeout(30000 /* milliseconds */);
                    conn.setConnectTimeout(50000 /* milliseconds */);
                    //conn.setRequestProperty("User-Agent", "GYUserAgentAndroid");
                    conn.setRequestProperty("Content-Type", "application/json");
                    //conn.setUseCaches (false);
                    // Starts the query
                    if (Build.VERSION.SDK != null && Build.VERSION.SDK_INT > 13) {

                        conn.setRequestProperty("Connection", "close"); 

                    System.out.println("Before url.openStream()");
                    InputStream stream = conn.getInputStream();//.openStream();
                    System.out.println("After url.openStream()");
                    String data = convertStreamToString(stream);
                    // for examole data = "1,2,3";


                } catch (Exception e) {

The final local variable cannot be assigned, since it is defined in an enclosing type?

The final local variable cannot be assigned, since it is defined in an enclosing type?

By : RedTiger
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further The cause of the problem is that final variables should be initialized when declared and cannot be reassigned. The best bet for this case will be to create inner field in the anonymous class that start with the final variables values, and this inner fields are updated periodically.
code :
CP.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    //Pepperoni Listener
    int innerTime = intTime;
    int innerCost = intCost;

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked1) {
        if(isChecked1) {
            innerTime += 6;
            innerCost += 5;
        } else {
            innerTime -= 6;
            innerCost -= 5;
The final local variable dc cannot be assigned, since it is defined in an enclosing type

The final local variable dc cannot be assigned, since it is defined in an enclosing type

By : user3292123
Date : March 29 2020, 07:55 AM
it should still fix some issue With Java, you cannot assign anything to a variable marked as final except in declaration. On the other hand, you need these variable to be final to be used in this closure. This may feel frustrating after JS. So what you may need to do with this particular code is to move variable declarations into closure, removing final modifier. If you still need to access them from outside, you are likely to define a class that implements Runnable interface, and make these variable to be fields of this class instances though get methods.
Also, do you really need this code to run asynchronously?
The final local variable cannot be assigned, since it is defined in an enclosing type

The final local variable cannot be assigned, since it is defined in an enclosing type

By : usman
Date : March 29 2020, 07:55 AM
I hope this helps you . Well, the standard trick is to use an int array of length one. Make the var final and write to var[0]. It is very important to make sure you don't create a data race. Using your code as an example:
Related Posts Related Posts :
  • Why onNext has no effect
  • IntelliJ Idea 2018.2 no option for importing gradle project
  • Remove a negative element from an array w/o array.copy
  • Could this prevent overflow in Java?
  • In Java Swing, can I receive Caret events in real time?
  • I need help not using brute force when using JFrames and DrawWindows
  • How do I refactor ArrayList<MyClass> into MyClassLIst?
  • My program keeps saying that the method cannot be resolved
  • BubbleSort -my code returns random addresses
  • JavaFX components inside HTML?
  • Character in Java game not responding
  • Working around access denied in a FileWalking Tree in Java7
  • How to avoid if/else when using multple suppliers?
  • How to pass object from table cell editor to Table Model?
  • Paypal Sandbox payment state pending
  • sejda-console.bat passing parameters with spaces
  • Multiple Consumers Spring Kafka
  • Bitbucket pipeline: environmental variables in build.gradle file not recognized
  • Java static enum method to return a default enum value
  • What is the complexity of empty for loop?
  • Group randomly List of Lists as N subsets with K elements in each
  • How to set texture on a shape drawn by glDrawArrays()?
  • How to make a jlabel resizable according to the window
  • Java Mathematical Expression Syntax
  • assigning values to characters in java
  • Method overriding and Inheritance in java
  • given a set of lists/groups with a series of number, find matching numbers
  • Connection to HID USB device (keyboard and mouse) in android
  • Inject HttpServletRequest in CDI SessionScoped bean
  • Pool game in java - ball collision algorithm
  • Focus ScrollView to selected position programmatically - Android
  • Is it hibernate bug?
  • Glib memory allocation error
  • Android Proguard - step by step
  • Setting the pivot point of a JPanel to its center
  • java check time is greater time
  • how to save a screenshot (matlab)
  • Anti-aliasing filled shapes in libgdx
  • Gwt custom text box having baloon popup
  • How to change date dynamically for each element in an Array
  • JPanel won't add the JLabel text?
  • how to access the .properties file in ant if it is in different location
  • Reduce application memory footprint
  • java.lang.ClassCastException: javax.mail.Session cannot be cast to javax.mail.Session
  • What is the exact purpose of calling System.exit() in java
  • How to do you get output from Javascript into Java using ScriptEngine
  • Java replaceAll() method to escape special characters
  • Java (Removing a 'keyword' from the alphabet)
  • Format BigDecimal in Spring
  • iterating checkbox values stored in array using EL
  • Java - Jackcess API with .accde(MS Access) format
  • Alternative for some of Struts 1 methods in Struts 2
  • javax.crypto JDK source code, again
  • Spring Roo - Command 'service' not found
  • antlr4: ATN version 2 expected 3
  • Deploy GlassFish using Netbeans generate java.net.MalformedURLException: Bad URL path
  • why is wait() called for loop in Thread.join() of Java?
  • Java JAXB marshall into DOM Document
  • Why JDBI 3 @ColumnName annotation doesn't work?
  • Java SimpleDateformatter with 10 decimals after the seconds, cannot convert to Date
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org