Search code examples
javapackagevisibility

How to get class'es method without importing class


There is a situation:

package pak1 contains some class

package pak1;

public class A {
    public void g() {}
}

and another package pak2

package pak2;

public class B {    
    public  void f() {
       // here I want to call method g() from class A
    }
}

Is there any way to call class's A method g() without importing class A (and then new A().g())?

If the method g() was static, I could write

public void f() {
    pak1.A.g();
}

Solution

  • you can use fully qualified class name like:

    pak1.A a = new pak1.A();
    a.g();