英文:
Why am I seeing the "cannot find symbol error" in java when creating an object from another class?
问题
以下是代码的翻译部分:
这是我的主类,名为Revenue,在这里我试图调用与此类在同一目录中并且在同一个包中的次要类
package Hotel;
//import java.util.*;
public class Revenue {
String Name;
Revenue()
{
Name = "";
}
public void Guest()
{
System.out.println("Revenue Guest. ");
Persons x = new Persons(); //这里实际上发生错误
System.out.println("Guest name:" + x.name() );
}
public static void main(String args[])
{
System.out.println("Revenue main.");
Revenue rev = new Revenue();
rev.Guest();
}
}
现在下面是次要类的部分:
package Hotel;
public class Persons {
String firstName[] = {"Adam","Alex", "Steve", "Sira"};
public Persons()
{
}
public String name() {
System.out.println("Persons name");
java.util.Random r = new java.util.Random();
String name = firstName[r.nextInt(firstName.length)];
return name;
}
public static void main(String[] args)
{
Persons n = new Persons();
System.out.println(n.name());
}
}
至于您提到的错误,请确保以下几点:
- 两个类都在同一个包(Hotel)中。
- 您在编译和运行代码时位于正确的目录中。
- 使用
javac Revenue.java
编译并使用java Revenue
运行主类。 - 如果使用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
package Hotel;
//import java.util.*;
public class Revenue {
String Name;
Revenue()
{
Name="";
}
public void Guest()
{
System.out.println("Revenue Guest. ");
Persons x = new Persons(); //Here's where the error is actually occuring
System.out.println("Guest name:" + x.name() );
}
public static void main(String args[])
{
System.out.println("Revenue main.");
Revenue rev = new Revenue();
rev.Guest();
}
}
Now the next part is secondary class
package Hotel;
public class Persons {
String firstName[] = {"Adam","Alex", "Steve", "Sira"};
public Persons()
{
}
public String name() {
System.out.println("Persons name");
java.util.Random r = new java.util.Random();
String name = firstName[r.nextInt(firstName.length)];
return name;
}
public static void main(String[] args)
{
Persons n = new Persons();
System.out.println(n.name());
}
}//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.
Revenue.java:18: error: cannot find symbol
Persons x = new Persons();
^
symbol: class Persons
location: class Revenue
Revenue.java:18: error: cannot find symbol
Persons x = new Persons();
^
symbol: class Persons
location: class Revenue
2 errors
error: compilation failed
It's been whole 24 hours but I'm not able to figure it out what's the issue here
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
一样。它应该可以工作。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论