Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

variables in initialization blocks.

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Where would variables initialized in initialization blocks be used? Since the scope would be resolved to the block only, these variables cannot be used outside I believe.

Larsen
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Larsen Raja wrote:Hi,

Where would variables initialized in initialization blocks be used? Since the scope would be resolved to the block only, these variables cannot be used outside I believe.

Larsen


Hello Larsen, I recommend you write a test program and do some 'trial-and-error' coding it is a good way to resolve such doubts.

HtH

Ikpefua
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variables declared or initialized in instance block? variables declared within it's scope can be used for computations = normal to what you use local variables in a method.
 
Larsen Raja
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried using such variables outside the block, say another method. But this brings up compilation error. The variable cannot be resolved outside the init block.


Larsen.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is exactly what you'd expect from any local variable - the scope is limited to the block they are declared in.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ikpefua Jacob-Obinyan wrote:

Larsen Raja wrote:Hi,

Where would variables initialized in initialization blocks be used? Since the scope would be resolved to the block only, these variables cannot be used outside I believe.

Larsen


Hello Larsen, I recommend you write a test program and do some 'trial-and-error' coding it is a good way to resolve such doubts.

HtH

Ikpefua


Good to see you back again, Ikpefua
 
I am not young enough to know everything. - Oscar Wilde This tiny ad thinks it knows more than Oscar:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic