为什么我不能将值传递给这些变量

时间:2013-01-26 00:00:44

标签: java eclipse variables scope

我正在使用eclipse构建Android Twitter应用程序 我试图将值传递给变量itemOfClothing,clothingEmotion和“user”,但是eclipse甚至不允许我使用相同的名称启动变量,更不用说将值传递给它们了。

我收到以下错误:

Duplicate field TwitterApp.itemOfClothing   
Duplicate field TwitterApp.clothingEmotion  
Duplicate field TwitterApp.user 
Syntax error on token ";", { expected after this token  
Syntax error, insert "}" to complete Block  

有人可以帮忙吗?

String itemOfClothing; //Item of clothing sending the message
String clothingEmotion; //Message of clothing
String user; //This comes from twitter sign in process

//将传递给变量的示例!

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

尝试将值传递到itemOfClothingclothingEmotion和“用户”,但它不会让我。我知道这是愚蠢的,但我不知道为什么。有人可以给我一个答案吗?

public static String MESSAGE = itemOfClothing +": " + clothingEmotion + "! #" + user + "EmotionalClothing";

3 个答案:

答案 0 :(得分:3)

静态变量只能引用其他静态变量。

private static String itemOfClothing; //Item of clothing sending the message
private static String clothingEmotion; //Message of clothing
private static String user; //This comes from twitter sign in process

答案 1 :(得分:2)

public static String MESSAGE = itemOfClothing +": " + clothingEmotion + "! #" + user + "EmotionalClothing";

因为

temOfClothingclothingEmotionuser非静态变量,您正在尝试将其分配给static variable MESSAGE,因此您的编译器会抱怨

  

无法对非静态字段用户进行静态引用

使它们成为静态变量,你的代码就可以了。

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


    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 String MESSAGE = itemOfClothing +": " + clothingEmotion + "! #" + user + "EmotionalClothing";

答案 2 :(得分:1)

您已将这些变量声明为成员变量,但您尝试使用静态初始化程序分配static字符串变量。

静态初始值设定项只能访问静态变量或文字值。

您可以做的是将变量声明为static:

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

请注意,如果不对这些变量使用静态初始值设定项,则它们将没有值。但是,如果要将这些值设置为程序逻辑的一部分,则要么声明字符串NOT不是静态字符串,要么使用语句来指定不是“初始化程序”的值(声明的一部分)。 / p>

或者:

public String MESSAGE = itemOfClothing +": " + clothingEmotion + "! #" + user + "EmotionalClothing";

或:

public static String MESSAGE;

//on another line as part of a program, after the variables get values
MESSAGE = itemOfClothing +": " + clothingEmotion + "! #" + user + "EmotionalClothing";
相关问题