• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Usage of Block

 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I had one doubt regarding the block which is there in the code. When i remove the block i am getting errors. Why the blocks are used here? I am having doubt regarding it. The block is { states.add("VA"P) ........}
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is an instance initializer block. It's a bit unusual to use this; normally you would initialize member variables in a constructor.

Read more about it here: Initializing Fields
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Statements cannot directly exist in the class body. They should be present inside a method, constructor or the initialization block which you have shown.

Read about initialization block - http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

Jespers' Jet fast
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First thing you can't put executable code(I am not sure if this is correct word) directly in to the class, I meant that you can declare variables. place static blocks, inner class just inside the class but can't put like below

this is why you are getting error when you remove the curly braces from the code, just put that block inside some method and remove the braces, it will work fine(off course you have to take care of scope of the "states").
Second thing what I can understand is that the block mentioned here just inside the class is working as Initialization block and will initialize the values before the constructor is called. So this is one of the way to initialize the values in the variable. However I am not sure, may be someone else can elucidate it better.
 
Suryanarayana Murthy Maganti
Greenhorn
Posts: 17
Hibernate Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper.
 
Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic