从另一个java类调用静态方法 [英] Calling static method from another java class

查看:99
本文介绍了从另一个java类调用静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从使用PHP转向Java并进行了查询。想要强调我是Java的初学者。

I've recently switched from working in PHP to Java and have a query. Want to emphasise I'm a beginner in Java.

基本上我在文件A(带有类A)​​工作,想要引用文件B中保存的静态方法(B类)。在使用A类时,是否需要对文件B进行任何引用? (我在思考PHP中的require_once)我在A类中的代码如下:

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:

Public class A{
String[] lists = B.staticMethod();
}

Eclipse没有将B识别为类。我是否需要创建B的实例才能访问静态方法。感觉我真的忽略了什么,并会感激任何输入。

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.

推荐答案

确保您有权访问B.staticMethod。也许声明为

Ensure you have proper access to B.staticMethod. Perhaps declare it as

public static String[] staticMethod() {
    //code
}

此外,您需要导入B类

import foo.bar.B; // use fully qualified path foo.bar.B

public class A {
    String[] lists = B.staticMethod();
}

这篇关于从另一个java类调用静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆