在Java中,当从另一个类创建对象时,为什么会出现“找不到符号错误”?

huangapple go评论100阅读模式
英文:

Why am I seeing the "cannot find symbol error" in java when creating an object from another class?

问题

以下是代码的翻译部分:

  1. 这是我的主类名为Revenue在这里我试图调用与此类在同一目录中并且在同一个包中的次要类
  1. package Hotel;
  2. //import java.util.*;
  3. public class Revenue {
  4. String Name;
  5. Revenue()
  6. {
  7. Name = "";
  8. }
  9. public void Guest()
  10. {
  11. System.out.println("Revenue Guest. ");
  12. Persons x = new Persons(); //这里实际上发生错误
  13. System.out.println("Guest name:" + x.name() );
  14. }
  15. public static void main(String args[])
  16. {
  17. System.out.println("Revenue main.");
  18. Revenue rev = new Revenue();
  19. rev.Guest();
  20. }
  21. }

现在下面是次要类的部分:

  1. package Hotel;
  2. public class Persons {
  3. String firstName[] = {"Adam","Alex", "Steve", "Sira"};
  4. public Persons()
  5. {
  6. }
  7. public String name() {
  8. System.out.println("Persons name");
  9. java.util.Random r = new java.util.Random();
  10. String name = firstName[r.nextInt(firstName.length)];
  11. return name;
  12. }
  13. public static void main(String[] args)
  14. {
  15. Persons n = new Persons();
  16. System.out.println(n.name());
  17. }
  18. }

至于您提到的错误,请确保以下几点:

  1. 两个类都在同一个包(Hotel)中。
  2. 您在编译和运行代码时位于正确的目录中。
  3. 使用javac Revenue.java编译并使用java Revenue运行主类。
  4. 如果使用VS Code,确保项目设置正确。

如果按照上述步骤操作仍然出现问题,请提供更多关于错误的详细信息,以便我能够更好地帮助您解决问题。

英文:

This is my primary class named Revenue where I'm trying to call a secondary class in same directory as this one and in the same package as well

  1. package Hotel;
  2. //import java.util.*;
  3. public class Revenue {
  4. String Name;
  5. Revenue()
  6. {
  7. Name="";
  8. }
  9. public void Guest()
  10. {
  11. System.out.println("Revenue Guest. ");
  12. Persons x = new Persons(); //Here's where the error is actually occuring
  13. System.out.println("Guest name:" + x.name() );
  14. }
  15. public static void main(String args[])
  16. {
  17. System.out.println("Revenue main.");
  18. Revenue rev = new Revenue();
  19. rev.Guest();
  20. }
  21. }

Now the next part is secondary class

  1. package Hotel;
  2. public class Persons {
  3. String firstName[] = {"Adam","Alex", "Steve", "Sira"};
  4. public Persons()
  5. {
  6. }
  7. public String name() {
  8. System.out.println("Persons name");
  9. java.util.Random r = new java.util.Random();
  10. String name = firstName[r.nextInt(firstName.length)];
  11. return name;
  12. }
  13. public static void main(String[] args)
  14. {
  15. Persons n = new Persons();
  16. System.out.println(n.name());
  17. }
  18. }//class

And yes it works fine on debug mode through vs code (Redhat java something). But when i try to do javac Revenue.java or java Revenue.java in cmd it shows an error like this.

  1. Revenue.java:18: error: cannot find symbol
  2. Persons x = new Persons();
  3. ^
  4. symbol: class Persons
  5. location: class Revenue
  6. Revenue.java:18: error: cannot find symbol
  7. Persons x = new Persons();
  8. ^
  9. symbol: class Persons
  10. location: class Revenue
  11. 2 errors
  12. error: compilation failed

It's been whole 24 hours but I'm not able to figure it out what's the issue here 在Java中,当从另一个类创建对象时,为什么会出现“找不到符号错误”?

I tried creating a program named revenue that will extract a persons name from another class named person(Just for the sake of future expansion of person class). And in BlueJ it seemed to work pretty fine without any error but recently I switched to cmd and vs code and here I'm stuck with this code where it wont let me call a method from another class.

答案1

得分: 0

只尝试使用命令 java className.java 进行编译和执行,就像 java Persons.java 一样。它应该可以工作。

英文:

Just try the command java className.java to compile and execute like java Persons.java. It should work.

在Java中,当从另一个类创建对象时,为什么会出现“找不到符号错误”?

huangapple
  • 本文由 发表于 2023年6月6日 03:18:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定