Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Final variables and try blocks

843789Jan 2 2010 — edited Jul 13 2010
If you tried to initialize a final variable within a try block, you would get a compile-time error.

Here's an example class:
class Example {
    private final String example;

    public Example() {
        try {
            example = "Example";
        } catch (Exception e) {
            // Just an example.
        }
    }
}
Obviously you wouldn't need to try-catch something like this (it's a stupid example - I know) , but the point is, the code wouldn't compile, and would return an error saying something like "example may not have been initialized".

So I've got two questions. Why is this, and is there any way around it?

Thanks in advance :)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 10 2010
Added on Jan 2 2010
15 comments
1,691 views