Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8206525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:42:12+00:00 2026-06-07T08:42:12+00:00

If I have a variable int x = 1 , say, and I declare

  • 0

If I have a variable int x = 1, say, and I declare a runnable in the main thread, and I want to pass x to the runnable’s run() method, it must be declared final. Why?

final int x = 0;//<----must be final...
private class myRun implements Runnable {

    @Override
    public void run() {
        x++;//
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:42:14+00:00Added an answer on June 7, 2026 at 8:42 am

    Because if they are able to be changed, it could cause a lot of problems, consider this:

    public void count()
    {
        int x;
    
        new Thread(new Runnable()
        {
            public void run()
            {
                while(x < 100)
                {
                    x++;
                    try
                    {
                        Thread.sleep(1000);
                    }catch(Exception e){}
                }
            }
         }).start();
    
         // do some more code...
    
         for(x = 0;x < 5;x++)
             for(int y = 0;y < 10;y++)
                 System.out.println(myArrayElement[x][y]);
     }
    

    This is a rough example but you can see where a lot of unexplained errors could occur. This is why the variables must be final. Here is a simple fix for the problem above:

    public void count()
    {
        int x;
    
        final int w = x;
    
        new Thread(new Runnable()
        {
            public void run()
            {
                int z = w;
    
                while(z < 100)
                {
                    z++;
                    try
                    {
                        Thread.sleep(1000);
                    }catch(Exception e){}
                }
            }
         }).start();
    
         // do some more code...
    
         for(x = 0;x < 5;x++)
             for(int y = 0;y < 10;y++)
                 System.out.println(myArrayElement[x][y]);
     } 
    

    If you want a more full explanation, it is sort of like synchronized. Java wants to prevent you from referencing one Object from multiple Threads. Here is a little bit about synchronization:

    • http://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html

    Hope this helped!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a C# variable and array: int variable_1 = 1; int[3]
I have a int variable witch holds the old battery value it is declared
I have declared a variable like this @{ int i = 1; } Now,
I have the next scenario: I define an int[][] variable in my main class.
Say I declare a header file with a variable: int count; Then in the
Say I have a global variable INT named X. Since X is global, we
Lets say I have an int variable n = 8. On most machines this
Let's say we have an array defined as a global variable. int array[] =
I have an array variable, say for an example:- int a[10]; And suppose i
Say I have some windows method and a struct: struct SomeStruct{ int foo; int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.