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

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

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());
    }
}

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

  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

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 在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:

确定