Java:startingPath 为“public static final”例外

发布于 2024-08-28 08:39:41 字数 755 浏览 8 评论 0原文

[已更新,对更改感到抱歉,但现在是真正的问题了] 对于 getCanonicalPath() 方法的异常,我无法在其中包含 try-catch-loop。我尝试先用方法解决问题,然后在那里声明值。问题是它是最终的,我无法更改它。那么如何将startingPath 设置为“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

[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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱你是孤单的心事 2024-09-04 08:39:41

您可以提供静态方法来初始化静态变量:

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);
    }
}

或者您可以在静态块中初始化变量:

public static final String startingPath;

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

编辑:在这种情况下,您的变量是静态,因此没有声明抛出异常的方法。仅供参考,如果变量是非静态的,您可以通过在构造函数中声明抛出的异常来实现此目的,如下所示:

public class PathHandler {

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

    public PathHandler() throws IOException {
        // other initialization
    }

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
    }
一身骄傲 2024-09-04 08:39:41

名字很好;你忘了声明类型。

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

解决这个问题后,您当然会意识到如何处理可能的 IOExceptionstartingPathfinal 的更难问题。一种方法是使用 static 初始值设定项:

JLS 8.7 静态初始化器

类中声明的任何静态初始值设定项都会在类初始化时执行,并且与类变量的任何字段初始值设定项一起可用于初始化该类的类变量。

 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;
 }

另一种方法是使用 static 方法(请参阅凯文·布洛克的回答)。这种方法实际上可以带来更好的可读性,并且是 Josh Bloch 在 Effective Java 中推荐的方法。


另请参阅

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

掩于岁月 2024-09-04 08:39:41

只需在静态块中初始化它(该变量是最终的)。您无法在声明变量时捕获异常。

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

记忆里有你的影子 2024-09-04 08:39:41

您缺少变量的类型,它应该是

public static void String startingPath ...

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

public static void String startingPath ...
狂之美人 2024-09-04 08:39:41
 public static final String startingPath = (new File(".")).getCanonicalPath();

您缺少变量 startingPath 的类型

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

You are missing the type of variable startingPath

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文