Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

main() method is static but swing frames are objects  RSS feed

 
Karan Hans
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does the following staement mean:
The main() method is static but swing frames are objects so you have to figure how to get your program out of static context. This can be done by creating the application as a class that extends JFrame. the main method then calls the class constructor in effect creating an instance of itself.
 
Alexander Kober
Ranch Hand
Posts: 32
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karan Hans wrote:what does the following staement mean:
The main() method is static but swing frames are objects so you have to figure how to get your program out of static context. This can be done by creating the application as a class that extends JFrame. the main method then calls the class constructor in effect creating an instance of itself.


Are you familiar with the concept of static methods and their scope?

Basically, all the above is saying that in a class



you can't invoke setVisible(boolean) from the main method as JFrame#setVisible(boolean) is bound to an instance of JFrame while you're running within the class' static scope. What you need to do is instanciate MyApp first:



Note: The actually correct way would be to invoke setVisible from the EDT, but i didn't want to make the example more complicated.
 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i found that post confusing as well. for one thing you don't "have to" extend JFrame
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!