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

  • SEARCH
  • Home
  • 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 7692839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:56:43+00:00 2026-05-31T20:56:43+00:00

I have a program which has a text box that the user can edit.

  • 0

I have a program which has a text box that the user can edit. When the user presses a button a Dialog box is created, displaying the user’s text and a confirm ‘yes/no’ option.

public void setIP(View v){
    //get the text inside the editor
    EditText et = (EditText) findViewById(R.id.editTextIP);
    final String IP = et.getText().toString();

    //create dialog 
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Set I.P. to : " + IP + " ?")
           .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    host = IP;
               }
           })
           .setNegativeButton("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
               }
           });
    AlertDialog alert = builder.create();
    alert.show();
}

This code works fine, but my first version would not compile. My IDE complained that I should make the string IP final, when It was not.

This got me thinking. What does final add in this case? What if, for example, I chose to keep a reference to the Dialog, and show it from another method. This means the method above will have returned. So how can the variable IP linger on?

Does the final keyword just ensure the value isn’t changed elsewhere, allowing the onClickListener to store the value of IP, knowing it will still be true? is the scope of IP increased?

  • 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-05-31T20:56:45+00:00Added an answer on May 31, 2026 at 8:56 pm

    Does the final keyword just ensure the value isn’t changed elsewhere, allowing the onClickListener to store the value of IP, knowing it will still be true? is the scope of IP increased?

    In a sense, yes (though this is really the “extent” rather than the “scope”: the “scope” is still just the program text between the declaration of IP and the } at the end of the function).

    Implementation-wise, essentially what is happening is that the new DialogInterface.OnClickListener() { ... } has an implicit field named IP that is automatically initialized during the object’s construction. The final modifier serves to protect the abstraction of there being a single IP variable, by ensuring that the local variable IP and the implicit field IP continue to refer to the same String instance.

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

Sidebar

Related Questions

I have a program which has some Textareas / Labels these can be anywhere
I have a C program which has a function call that is defined in
Supposedly I have this line in my text file which has the following format.
I have created a table and there is some text in which column of
I have a Textblock which has an initial amount of text and height of
I have a program in C# that has a call: myEmail.SendMailPopup(Hi, This is my
I have a program developed under java with netbeans. It has a text pane
I have a small program that which is confusing me. I am trying using
I have a text editor I made, which has been working perfectly for the
BeyondCompare has this conversion feature which can run an external program on certain file

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.