java - Methods present in Frame class, which extends JFrame, can't be invoked in main() -
the code given below gives error (cannot make static reference non-static method setvisible(boolean) type window
) :
import javax.swing.jframe; public class frame extends jframe{ public static void main(string[] args) { setvisible(true); } }
while 1 compiles fine :
import javax.swing.jframe; public class frame extends jframe{ frame() { setvisible(true); } }
when frame extends jframe
, means frame inherits methods jframe (loosely saying), including setvisible(boolean)
. why can't invoke setvisible(true)
in main()
, while can in other methods?
the clue in exception message.
the setvisible
method instance method on jframe
in public static void main
, in static context, there no instance of frame
call setvisible
on.
you do:
public static void main(string[] args) { new frame().setvisible(true); }
because have instance
https://docs.oracle.com/javase/tutorial/java/javaoo/classvars.html might help
Comments
Post a Comment