简体   繁体   English

将方法从一个Java类调用到另一个Java类时遇到麻烦

[英]Trouble in invoking a method from one java class to another

I have a folder PackagesAndMethods . 我有一个文件夹PackagesAndMethods Within that folder I had two files 在那个文件夹中,我有两个文件

  1. TestMethods.java TestMethods.java
  2. MyMethods.java MyMethods.java

The code within those files are, 这些文件中的代码是

TestMethod.java TestMethod.java

package PackagesAndMethods;

public class TestMethods
{
    public static void main(String args[])
    {       
        int result = MyMethods.Total();
        System.out.println(result);
    }
}

MyMethods.java MyMethods.java

package PackagesAndMethods;

public class MyMethods
{
    public static int Total()
    {
        return 10;
    }
}

The problem is the "MyMethods.java" class compiles successfully but when compiling the "TestMethods.java", i am getting the bellow error 问题是“ MyMethods.java”类编译成功,但是在编译“ TestMethods.java”时,出现了以下错误

error: cannot find symbol
                int result = MyMethods.Total();
                             ^
  symbol:   variable MyMethods
  location: class TestMethods
1 error 

What I am doing wrong? 我做错了什么?

The problem is how you're compiling. 问题是您如何编译。 You should generally be compiling from the "package root", ideally specifying an output root as well. 通常,您应该从“程序包根目录”进行编译,最好也指定输出根目录。 For example, from the parent directory ( D:\\Java ): 例如,从父目录( D:\\Java ):

> javac -d classes PackagesAndMethods\MyMethods.java
> javac -d classes -cp classes PackagesAndMethods\TestMethods.java

Or more simply: 或更简单地说:

> javac -d classes PackagesAndMethods\*.java

Currently the compiler is expecting to find a directory called PackagesAndMethods to look in for classes in the PackagesAndMethods directory. 当前,编译器期望找到一个名为PackagesAndMethods的目录,以在PackagesAndMethods目录中查找类。

I'd personally separate the source into its own separate directory to keep it well away from the output, so you end up with: 我将源单独分隔到其自己的单独目录中,以使其与输出保持良好的距离,因此最终得到:

> javac -d classes src\PackagesAndMethods\*.java

You're likely to find it simpler to start with if you work in an IDE which manages all of this for you though. 如果您在IDE中工作,那么您可能会发现它更简单,尽管它可以为您管理所有这些工作。 It's still worth separating out the source and output. 仍然值得将源和输出分开。

First you need to compile MyMethods.java first. 首先,您需要首先编译MyMethods.java。 then Compile TestMethod.java. 然后编译TestMethod.java。 then run TestMethod. 然后运行TestMethod。

I think you are compiling TestMethod.java first. 我认为您是先编译TestMethod.java。 so compiler not able to find MyMethods.class file. 因此编译器无法找到MyMethods.class文件。

If I compile with this command: 如果我使用以下命令进行编译:

javac.exe PackagesAndMethods*.java javac.exe PackagesAndMethods * .java

the files is compiled without any errors 文件编译没有任何错误

variable MyMethods is not declared, you should create an instance of class MyMethods as MyMethods myMethods = new MyMethods(); 未声明变量MyMethods,则应创建类MyMethods的实例,作为MyMethods myMethods = new MyMethods(); and use myMethods.Total(); 并使用myMethods.Total(); (remember, instances should be lower case, classes uppercase, methods lowercase (请记住,实例应为小写字母,类应为大写字母,方法应为小写字母

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM