Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6827221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:14:26+00:00 2026-05-26T22:14:26+00:00

There seems to be two accepted variable declaration placements for Java variables, each with

  • 0

There seems to be two accepted variable declaration placements for Java variables, each with different raison d’être.

From the Sun’s code conventions we can see:

Put declarations only at the beginning of blocks. (A block is any
code surrounded by curly braces “{” and “}”.) Don’t wait to declare
variables until their first use; it can confuse the unwary programmer
and hamper code portability within the scope.

However in the highly praised “Code Complete” and some other authors advocate for reducing the scope of a variable to a minimum. That is basically waiting to declare variables until their first use.

These two approaches are clearly contradictory although I can see the point for both of them.

Which should I follow? Is there any consensus on the matter?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:14:26+00:00Added an answer on May 26, 2026 at 10:14 pm

    Variables should be declared as close to their use as possible, for two reasons:

    • Vertical locality
    • Tool hinting

    Vertical locality makes it easier to reason about chunks of code. The less scanning a reader has to do the easier it is to understand what code does, and what side-effects it

    Reducing variable scope also allows better tool hinting for automated tools, like refactoring. The closer together related things are the more obvious they are.

    That said: if a method is long enough that the above points come in to play, it’s likely the method is already too long, and should be refactored anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There seems to be two styles of folder structure in the core Magento when
When invoking a WCF service asynchronous there seems to be two ways it can
It seems that these two operators are pretty much the same - is there
It seems to me that there are two scenarios in which to use JOINs:
Reading some Verilog code, there seem to be two ways of defining arguments in
Is there a commonly-accepted practice for designing java objects that are frequently loaded and
There seems to be two trends on this topic: Some answers (such as this
It seems there are two methods for this, the first one goes like this:
To get an application installed on a new computer there seems to be two
After setting up a repo on Github, there seems to be two ways to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.