Java:startingPath 为“public static final”例外
[已更新,对更改感到抱歉,但现在是真正的问题了] 对于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以提供静态方法来初始化静态变量:
或者您可以在静态块中初始化变量:
编辑:在这种情况下,您的变量是
静态
,因此没有声明抛出异常的方法。仅供参考,如果变量是非静态的,您可以通过在构造函数中声明抛出的异常来实现此目的,如下所示:You can provide a static method to initialize your static variable:
Or you can initialize your variable in a static block:
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:名字很好;你忘了声明类型。
解决这个问题后,您当然会意识到如何处理可能的
IOException
和startingPath
为final
的更难问题。一种方法是使用static
初始值设定项:JLS 8.7 静态初始化器
另一种方法是使用
static
方法(请参阅凯文·布洛克的回答)。这种方法实际上可以带来更好的可读性,并且是 Josh Bloch 在 Effective Java 中推荐的方法。另请参阅
The name is fine; you forgot to declare the type.
Fixing that, you of course realize the harder problem of how to deal with the possible
IOException
andstartingPath
beingfinal
. One way is to use astatic
initializer:JLS 8.7 Static Initializers
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
只需在静态块中初始化它(该变量是最终的)。您无法在声明变量时捕获异常。
Just initialize it in the static block(the variable is final). You can't catch exceptions on declaring a variable.
您缺少变量的类型,它应该是
You are missing the type of your variable, it should be
您缺少变量
startingPath
的类型You are missing the type of variable
startingPath