Dr.G Dr.G - 5 months ago 19
Java Question

How to call the main method of another class from the main class in Java?

How do I call a "class" of the JFrame type for example from my main class? My JFrame has its own main giving setVisible true for him to appear, but I wanted to access this main it from my main class in order to give setVisible by his main instead I create an instance of such and give the setVisible for my class, because lose quality frame design.

Answer

The main method in your JFrame type class is a static one, right? Its prototype is

public static void main(String[] args){}

right? If so, you can call it just the way you call a (non-private) static method of any class.

Classname.main();

This is how you'll call the method. If the class name, say, is Myclass extends JFrame, then this is how you'll call the main() method:

Myclass.main();

Doesn't matter whether the class is of JFrame type. You can call main method of other classes from your main class (from a static method of the class) in this manner. Hope its clear.

Comments