为什么我在代码中出现这些括号错误?

时间:2013-01-26 09:32:54

标签: java eclipse syntax static-members brackets

我真的不确定为什么会这样做,但似乎是括号内的一个问题。 我在Eclipse中运行Android的这段代码时遇到以下错误:

private static final String TWITTER_ACCESS_TOKEN_URL = "http://api.twitter.com/oauth/access_token";
private static final String TWITTER_AUTHORZE_URL = "https://api.twitter.com/oauth/authorize";
private static final String TWITTER_REQUEST_URL = "https://api.twitter.com/oauth/request_token";

public static final String itemOfClothing;
public static final String clothingEmotion;
public static final String user;<<<<<<<<<<<<<<<<<<<<< Syntax error on token ";", { expected after this token


itemOfClothing = "pants";
clothingEmotion = "I'm feeling left in the dark";
user = "stuart";

public static String MESSAGE = itemOfClothing +": " + clothingEmotion + "! #" + user + "EmotionalClothing"; <<<<<<<<<<<<<<<<<<<<<< Syntax error, insert "}" to complete Block


public TwitterApp(Activity context, String consumerKey, String secretKey) {
    this.context = context;

1 个答案:

答案 0 :(得分:2)

您应该仅在声明点或构造函数内初始化字符串。你不能在顶级课堂上发表演讲。你可以在那里声明。

所以,一个解决方案是,改变以下陈述: -

public static final String itemOfClothing;
public static final String clothingEmotion;
public static final String user;

/** You can't have below assignments directly under the top-level class **/
itemOfClothing = "pants";
clothingEmotion = "I'm feeling left in the dark";
user = "stuart";

来: -

public static final String itemOfClothing = "pants";
public static final String clothingEmotion = "I'm feeling left in the dark";
public static final String user = "stuart";

或者,另一种解决方案是,在构造函数中移动这些赋值,在这种情况下,您还必须在该构造函数中移动initialization MESSAGE

而且,如果这些变量应该是constants,我认为它们是public static final,那么你应该使用ALL_CAPS_WITH_UNDERSCORE来命名它们。