So I am a newbie in Java and reading the Head First Java (2nd Edition). I am doing the example on page 392 (chapter 12). Here is an excerpt of the code from the book. I have added one line of code "f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);" to close the Frame window once code run is complete but I keep getting the compile error.
But when I place this line of code (at the shown location - code against the comment line), code runs just fine. I just don't understand why so if someone can provide an explanation, that would be awesome!
As Knute already answered your question, I will just give an additional point. You could actually do this(placing statements outside methods or constructors) with Initialization blocks in Java. There are two types of Initialization blocks; Instance & Static. These blocks will run before constructors will execute. Please have a visit to this site.
Number are the order of execution.
I hope you have got some knowledge about init blocks.
OCAJP Java 8, OCPJP Java 8
I do some of my very best work in water. Like this tiny ad: