Can a main() be accessed directly from outside the class in which it is defined? Like for example:
class B { main(); } class A { public static void main(String arg[]) { int a=10; int b=20; System.out.println(a+" "+b); } } You can call main method from other class as well because its static.no object reference is require to call static method, but you should call it from other method like below.
public class B { public static void main(String[] args) { String[] s = {"a"}; A.main(s); } } class A { public static void main(String arg[]) { int a=10; int b=20; System.out.println(a+" "+b); } }
mainfrom classB, you would need an instance ofB, something likeB b = new B();, then you can usebto callmain, something likeb.main();A.mainbecausemainis astaticmethod.