10

I've recently switched from working in PHP to Java and have a query. Want to emphasise I'm a beginner in 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:

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.

LeDoc
  • 935
  • 2
  • 12
  • 24
  • 1
    Are those classes in different packages? If yes then you need to import `B` class in `A` class. – Rohit Jain Sep 16 '13 at 17:37
  • 1
    static methods do not require an instance. Everything looks fine, however the `public` keyword should start with a lowercase 'p'. If `B` is in another package, you need to add an import statement: `import package_of_B.B;` – RaptorDotCpp Sep 16 '13 at 17:37

4 Answers4

17

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

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

Also, you need to import class B

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

public class A {
    String[] lists = B.staticMethod();
}
cmd
  • 11,622
  • 7
  • 51
  • 61
  • hello, can I know the reason why we have to use `static` if we want to call the method from one class to another class – Jems Apr 09 '18 at 03:00
  • Why can't we have something like `import foo.bar.B.staticMethod;` and then directly use it as `String[] lists = staticMethod();` ? – paradocslover Apr 28 '21 at 07:35
2

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();
}
BlakeP
  • 429
  • 4
  • 8
1

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.

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.

For example, code in a program that works with the ArrayList and Date classes could be written like this:

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 java.util.*;
....
ArrayList<Date> list = new ArrayList<>();
list.add(new Date());
Joni
  • 108,737
  • 14
  • 143
  • 193
0

You can also import the static method only, like this:

import static packageX.packageY.B.staticMethod;

Then call the method directly from inside your class method A without using a class name, like this:

String[] lists = staticMethod();