• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Need clarification and explanation

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was it!! .. Thank you so much for your response. I will keep that in mind.

Thanks!
Vj
 
Ranch Hand
Posts: 135
5
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I do some of my very best work in water. Like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic