This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Coding conventions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Coding conventions" Watch "Coding conventions" New topic

Coding conventions

Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127

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:

Mark Smyth
Ranch Hand

Joined: Feb 04, 2004
Posts: 288
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.

Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916


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

I agree. Here's the link:
subject: Coding conventions
Similar Threads
confuse statement ?
comparing and sorting arraylist of arraylist
Updated MulitThreads Problem
Multi-dimensional arrays
problem in parsing an XML using SAX parser.