views:

157

answers:

5

[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
+4  A: 

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

polygenelubricants
I don't see which parts are: initialization, declaration and assignment. It is hard to understand the term final without focusing on them separately.
HH
I think Kevin Brock's `private static` method solution provides the most readability. I recommend using that approach. That's also what Josh Bloch recommends in _Effective Java_ (see the question I linked to); I don't think it's ethical of me to incorporate that late into my answer after his (unless he gives me permission to).
polygenelubricants
Big thanks, more I read it, more I like it.
HH
A: 

You are missing the type of your variable, it should be

public static void String startingPath ...
Jack
A: 
 public static final String startingPath = (new File(".")).getCanonicalPath();

You are missing the type of variable startingPath

Upul
+2  A: 

Just initialize it in the static block(the variable is final). You can't catch exceptions on declaring a variable.

Petar Minchev
+4  A: 

You can provide a static method to initialize your static variable:

public static final String startingPath = initPath();

private static String initPath() {
    try {
        return new File(".").getCanonicalPath();
    } catch (IOException e) {
        throw new RuntimeException("Got I/O exception during initialization", e);
    }
}

Or you can initialize your variable in a static block:

public static final String startingPath;

static {
    try {
        startingPath = new File(".").getCanonicalPath();
    } catch (IOException e) {
        throw new RuntimeException("Got I/O exception during initialization", e);
    }
}

EDIT: In this case your variable is static so there is no way to declare the exception thrown. Just for reference, if the variable is non-static you could do this by declaring the thrown exception in the constructor, like so:

public class PathHandler {

    private final String thePath = new File(".").getCanonicalPath();

    public PathHandler() throws IOException {
        // other initialization
    }
Kevin Brock
I'm usually throwing ExceptionInInitializerError.
lexicore