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.
JavaScript
x
package mypkg;
public class hai {
void display() {
System.out.println("Hello world");
}
}
and the next class is
JavaScript
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 ?
Advertisement
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.