从另一个java类调用静态方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18834005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling static method from another java class
提问by LeDoc
I've recently switched from working in PHP to Java and have a query. Want to emphasise I'm a beginner in Java.
我最近从 PHP 工作切换到 Java 并有一个查询。想强调一下我是 Java 的初学者。
Essentially I am working in File A (with class A) and want to refer to a static method saved in File B (class B). Do I need to make any reference to File B when working with class A? (I'm thinking along the lines of require_once in PHP) My code in Class A is as follows:
本质上,我在文件 A(类 A)中工作,并希望引用保存在文件 B(类 B)中的静态方法。使用 A 类时是否需要引用文件 B?(我正在考虑 PHP 中的 require_once )我在 A 类中的代码如下:
Public class A{
String[] lists = B.staticMethod();
}
Eclipse is not recognising B as a class. Do I need to create an instance of B in order to access the static method. Feel like I'm really overlooking something and would appreciate any input.
Eclipse 没有将 B 识别为一个类。我是否需要创建 B 的实例才能访问静态方法。感觉我真的忽略了一些东西,并且希望得到任何输入。
采纳答案by cmd
Ensure you have proper access to B.staticMethod. Perhaps declare it as
确保您可以正确访问 B.staticMethod。也许将其声明为
public static String[] staticMethod() {
//code
}
Also, you need to import class B
此外,您需要导入 B 类
import foo.bar.B; // use fully qualified path foo.bar.B
public class A {
String[] lists = B.staticMethod();
}
回答by BlakeP
You don't need to create an instance of the class to call a static method, but you do need to import the class.
您不需要创建类的实例来调用静态方法,但确实需要导入类。
package foo;
//assuming B is in same package
import foo.B;
Public class A{
String[] lists = B.staticMethod();
}
回答by Joni
Java has classloader mechanism that is kind of similar to PHP's autoloader. This means that you don't need anything like a include
or require
function: as long as the classes that you use are on the "classpath" they will be found.
Java 有类加载器机制,有点类似于 PHP 的自动加载器。这意味着你不需要像 ainclude
或require
函数这样的东西:只要你使用的类在“类路径”上,它们就会被找到。
Some people will say that you have to use the import
statement. That's not true; import
does nothing but give you a way to refer to classes with their short names, so that you don't have to repeat the package name every time.
有些人会说你必须使用import
语句。这不是真的; import
除了给你一种用它们的短名称来引用类的方法之外什么都不做,这样你就不必每次都重复包名。
For example, code in a program that works with the ArrayList
and Date
classes could be written like this:
例如,与ArrayList
和Date
类一起工作的程序中的代码可以这样编写:
java.util.ArrayList<java.util.Date> list = new java.util.ArrayList<>();
list.add(new java.util.Date());
Repeating the package name gets tiring after a while so we can use import
to tell the compiler we want to refer to these classes by their short name:
重复包名一段时间后会很累,所以我们可以import
用来告诉编译器我们想通过它们的短名称来引用这些类:
import java.util.*;
....
ArrayList<Date> list = new ArrayList<>();
list.add(new Date());