【问题标题】:Cannot find symbol in java, both .java files are in the same directory when compiling在java中找不到符号,编译时两个.java文件都在同一个目录中
【发布时间】:2014-03-14 00:38:44
【问题描述】:

我有 2 个文件,tweet.java 和 Account.java。当我编译 tweet.java 时,它通过了,但是当我编译 Account.java 时,它说这个错误

C:\Users\Nolan\Documents\OBJECTP\Java\tweet>javac tweet.java

C:\Users\Nolan\Documents\OBJECTP\Java\tweet>javac Account.java
Account.java:8: error: cannot find symbol
    private tweet[] UserTweets;
            ^
symbol:   class tweet
location: class Account
Account.java:10: error: cannot find symbol
    private final tweet[] EMPTY_TWEET;
                  ^
symbol:   class tweet
location: class Account
Account.java:17: error: cannot find symbol
    this.UserTweets = new tweet[20];
                          ^
symbol:   class tweet
location: class Account
Account.java:18: error: cannot find symbol
    this.EMPTY_TWEET = new tweet[0];
                           ^
symbol:   class tweet
location: class Account
Account.java:38: error: cannot find symbol
            this.UserTweets[TweetCount] = new tweet(message);
                                              ^
symbol:   class tweet
location: class Account
5 errors

我问过我的教授和很多朋友,他们都不知道问题出在哪里!我在这里处于优势地位,因为我不得不使用我大学实验室的编译器(显然当我编译它时它在那里工作)。

我会把源代码中的方法删减一下。

tweet.java 文件:

 //tweet.java
 import java.util.Calendar;

public class tweet {

    private String message;
    private int D;
    private int M;
    private int Y;
    private int H;
    private Calendar now;
    private int Min;

    public tweet(String Mes){
        this.message=Mes;
        this.now=Calendar.getInstance();
        this.D=now.get(Calendar.DATE)+1;
        this.M=now.get(Calendar.MONTH);
        this.Y=now.get(Calendar.YEAR);
        this.H=now.get(Calendar.HOUR);
        this.Min=now.get(Calendar.MINUTE);
    }
    //Method's here
}

Account.java 文件:

//Account.java file
import java.util.Calendar;
public class Account {

    private String Username;
    private String Password;
    private String FirstName;
    private String LastName;
    private tweet[] UserTweets;
    private int TweetCount;
    private final tweet[] EMPTY_TWEET;

    public Account(String UserName,String Password,String FName,String LName){
        this.Username=UserName;
        this.Password=Password;
        this.FirstName=FName;
        this.LastName=LName;
        this.UserTweets = new tweet[20];
        this.EMPTY_TWEET = new tweet[0];
        this.TweetCount=0;
    }
//Method's here
}

你能帮帮我吗?

【问题讨论】:

  • 这应该可以。您的课程中是否有包声明?
  • 我已经执行了你的命令,没有问题。您的 tweet 类位于项目的根目录中,而不是在另一个包中?
  • 编辑:我尝试添加包声明,但没有成功。

标签: java compiler-errors classpath javac symbols


【解决方案1】:

虽然你已经把它们放在同一个目录下,但是这两个类是不能互相访问的。你需要在Account类中导入tweet或者更好的创建一个包,然后把两个类都放在里面。

com 包

导入 java.util.Calendar;

公开课推文{ ///

}

com 包

//Account.java 文件

导入 java.util.Calendar;

公开课帐号{

//

}

避免使用将类放在默认包中以获得更好的命名空间

【讨论】:

  • 我已经尝试创建一个包,但它没有工作。---- package twitter;导入 java.util.Calendar; public class Account { // code }-----在两个源文件上都这样做了。
猜你喜欢
  • 2013-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
相关资源
最近更新 更多