Skip to content
Advertisement

The method from the type is not visible error when i am using two different packages

I am totally new to programming.So my question is i tried to create two classes added them in to two seperate packages.but after i tried to call one of them in to another it shows the method display() from the type hai is not visible.

package mypkg;

public class hai {

    void display() {
        System.out.println("Hello world");
    }
}

and the next class is

package mypkg2;

import mypkg.hai;

public class hello {
    
    hello(){
        hai a=new hai();
        a.display();
        
    }
    
    public static void main(String[] args) {
        
        hello b=new hello();
    }
    

}

Here i tried to call hai class from mypkg and tried to run display function and print Hello world . but getting the mentioned error.so how to use this class properly to display Hello world from the other class ?

Answer

The default scope for a java method is package-private. So it will only be visible to classes in the same package.

Try defining it as a public method.

Advertisement