FAQ: Learn Java: Manipulating Variables - final Keyword

This community-built FAQ covers the “final Keyword” exercise from the lesson “Learn Java: Manipulating Variables”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Build Basic Android Apps with Java

Learn Java

FAQs on the exercise final Keyword

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Dear sir/ma’am
When I put the final keyword it does not work :frowning:
I do not get an error
final 1
What do I do?

1 Like

Hello,

You need to define a new variable for pi. You simply updated pi from 3.14 to 3.142 so you will not get an error because you are allowed to define pi as any value you like.

What you need to do on a new line define pi as a new value e.g 3.12 then you will get an error.

public class Final {
public static void main(String args) {
final double pi = 3.14;
System.out.println(pi);
pi = 3.15;

}

}

Final.java:5: error: cannot assign a value to final variable pi
pi = 3.15;
^
1 error

1 Like

I understood it! Thank you very much! :grinning_face_with_smiling_eyes:

I am barely starting my Java journey, so I have a question for the community and more advanced folks:

Do we have to use uppercase letters for variables when using the “final” keyword?

I couldn’t find a definitive answer, on one side I saw in the Oracle University Java Explorer course that we have to do it as a convention, also we need to use underscores to sepparate the variable name’s words, something like this:
final int MAX_COUNT = 30;

On the other hand I have seen some posts in StackOverflow that say this is not mandatory.
Thanks in advance!

Why does the value of pi update even if it is final? Isn’t it supposed to error?

When make something like this:

public class Final {
public static void main(String args) {
final double pi = 3.14;

  System.out.println(pi);

  //forgotten semicolon
  pi = 3.1416
	
}

}

Code does not work, due semicolon issue, but task 2. checkbox is checked out anyway.
I think this lesson should evaluate this error before checkbox status changes.

You didn’t change the pi variable, you created a new variable and used pi to define the new one.
Try something like += or -= on the pi variable to get the error.

I know how to get the error, but I was messing around and printed a change like this:


I’ve also seen others with similar screenshots, and I’m wondering about the trailing 00000001- Why does that happen??