Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 73
Registered: ‎11-02-2009
My Device: Not Specified
Accepted Solution

Java syntax question

I'm sure this is very trivial,  but I've only seen this syntax used in the context of BlackBerry development:

 

//default gauge
    final GaugeField gf1;
    {
      gf1 = new GaugeField();
      gf1.setLabel("default gauge:");
      gf1.setValue(12);
      fields.addElement(gf1);
    }



What is the purpose of the curly brackets here? There is no method/condition/iteration header preceeding it, so what purpose does it serve in the code? I've seen this used in BlackBerry tutorials and books. I can't seem to find any details on it's use. Can anyone shed some light on this or point me at material that covers this syntax? trivial though it probably is...

 

Developer
Posts: 172
Registered: ‎04-19-2010
My Device: Bold 9700

Re: Java syntax question

in C this is called a compound statement. within this block, all defined variables are only valid within this block. thats all.
cu
michael
--------------------------------------------------------------------------------------------------------------------------
please give kudos to thank everyone who helped you or put you in the right direction.
please mark the thread as solved, if you found your solution.
Developer
Posts: 73
Registered: ‎11-02-2009
My Device: Not Specified

Re: Java syntax question

Thanks, I had a gut feeling it was to do with scope. I just don't see it used all that frequently.

New Contributor
Posts: 3
Registered: ‎07-31-2011
My Device: Curve 8250
My Carrier: MTN Nigeria

Re: Java syntax question

Take note, that the only variable defined in this sequence of statements, occurs outside of the compund statement. 

Highlighted
Developer
Posts: 73
Registered: ‎11-02-2009
My Device: Not Specified

Re: Java syntax question

[ Edited ]

Yeah that is what confused me in the first instance.

 

I've seen anonymous blocks in the past, but usually I treat that as a code smell in which you should create another method i.e initializeGuiComponents();

 

A lot fo the examples I seen in BlackBerry books/tutorials simply define a reference of some kind, create an anonymous block, initialise it and start doing things. This doesn't seem to be good OOP in my opinion. There must be some benefit to doing it over creating a specific method for initialising components as it seems very common in BlackBerry development as far as I can see, not that I've seen it in our own app mind you...