Results 1 to 5 of 5
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    307
    Rep Power
    2

    Default Declaring variables: Inside or outside the main method?

    hey, this was just a simple question; should I declare variables (ints, strings, booleans) inside or outside my main method? Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Declaring variables: Inside or outside the main method?

    The main method should be short as can be and should pretty much function just to set the program going, that's it.

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    307
    Rep Power
    2

    Default Re: Declaring variables: Inside or outside the main method?

    Quote Originally Posted by Fubarable View Post
    The main method should be short as can be and should pretty much function just to set the program going, that's it.
    So, I should always declare variables that I will use in the main method inside or outside of it?

  4. #4
    jayamm is offline Member
    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Default Re: Declaring variables: Inside or outside the main method?

    If the variables you want to declare are ONLY used in the main method then it is good practice to keep them inside that method. However, as Fubarable said earlier, the main method is typically very short.
    Last edited by jayamm; 01-14-2013 at 08:42 PM.

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Declaring variables: Inside or outside the main method?

    From your question I do not think you understood the concept of object orientation or even earlier concepts. I think you should read on what it is about and why it is like that. Why and how variables are declared in different scopes and where it makes sense to declare them and where not. Doing a tutorial step by step should help or you may read about what there is here: Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)
    I like likes!

Similar Threads

  1. Declaring variables on the fly
    By sihyde in forum New To Java
    Replies: 2
    Last Post: 04-20-2012, 02:35 PM
  2. Question about declaring variables
    By salmontres in forum New To Java
    Replies: 2
    Last Post: 12-18-2011, 12:58 AM
  3. Declaring a method inside a method: Runnable?
    By africanhacker in forum New To Java
    Replies: 8
    Last Post: 03-23-2011, 04:30 AM
  4. Declaring variables
    By Flashmatix in forum New To Java
    Replies: 12
    Last Post: 01-20-2009, 03:43 PM
  5. Declaring global variables
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 12:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •