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 716175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:16:10+00:00 2026-05-14T05:16:10+00:00

[Updated, sorry about the change but now to the real problem] I cannot include

  • 0

[Updated, sorry about the change but now to the real problem]
I cannot include try-catch-loop there for the exception from the method getCanonicalPath(). I tried to solve the problem earlier with method and then declaring the value there. The problem is that it is Final, I am unable to change it. So how to have startingPath as “public static final”.

$ cat StartingPath.java 
import java.util.*;
import java.io.*;

public class StartingPath {
 public static final String startingPath = (new File(".")).getCanonicalPath();

 public static void main(String[] args){
  System.out.println(startingPath);
 }
}
$ javac StartingPath.java 
StartingPath.java:5: unreported exception java.io.IOException; must be caught or declared to be thrown
 public static final String startingPath = (new File(".")).getCanonicalPath();
                                                                           ^
1 error
  • 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-14T05:16:11+00:00Added an answer on May 14, 2026 at 5:16 am

    The name is fine; you forgot to declare the type.

    public static final String startingPath;
    //                  ^^^^^^
    

    Fixing that, you of course realize the harder problem of how to deal with the possible IOException and startingPath being final. One way is to use a static initializer:

    JLS 8.7 Static Initializers

    Any static initializers declared in a class are executed when the class is initialized and, together with any field initializers for class variables, may be used to initialize the class variables of the class.

     public static final String startingPath;
     static {
        String path = null;
        try {
          path = new File(".").getCanonicalPath();
        } catch (IOException e) {
          // do whatever you have to do
        }
        startingPath = path;
     }
    

    Another way is to use a static method (see Kevin Brock’s answer). That approach actually results in better readability, and is the recommended approach by Josh Bloch in Effective Java.


    See also

    • How to handle a static final field initializer that throws checked exception?
    • In what order do static initializer blocks in Java run?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry about the question title, but I couldn't find a more appropriate way to
I read about adjustor thunk from here . Here's some quotation: Now, there is
Sorry, but I can only show this problem on a live sandbox site. if
update: I know there is no one best way to do everything. Sorry for
I've updated my TortoiseSVN client and now I'm getting the error when trying to
First of all, sorry for the too long question. I know that there are
Sorry if the title is not very clear. I was not sure about the
OK, sorry about the bad pun :P I've coded the old trick of HAL
(ps: sorry about my english) I have a text field in my application and
I need to change the database connection string in the web.config file but havent

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.