This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need clarification and explanation  RSS feed

 
Vijayendra Shukla
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



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!

 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

You have a statement outside of a method or a constructor. You can only have declarations and possible initializations there. Move the line inside a method.
 
Vijayendra Shukla
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was it!! .. Thank you so much for your response. I will keep that in mind.

Thanks!
Vj
 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Ranch, Vijayendra !

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.

Regards,
Jude
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!