• 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

Coding conventions

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

I'm almost finishing coding and checking what I wrote in the mean time. I have a simple question about declaring a reference. Sun says we should create object in the smallest scope needed for the object, that's ok but what about reference? Which one is more appropriate? Pay attention to "String [] myArray" declaration please.


Snippet one:



Snippet two:

 
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is the location of the the reference declaration that determines the scope of an object not where you create the object. In your example the second option is better as the variable is declared and only in the block of code that it is used in. This makes the code easier to understand.
 
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

The specs from sun


create object in the smallest scope needed for the object, that's ok but what about reference



is a line to follow not a must. You must decide where it fits and where in does not.
IMHO If the myArray is used in other palaces also then 1 otherwise 2.

Regards M
reply
    Bookmark Topic Watch Topic
  • New Topic