Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Variable declaration placement

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Variable declaration placement

    I'm working on a slightly intricate GUI program with a lot of JButtons, JTextFields, and the sort. In classes, all the examples were written as such:

    public class Foo
    {
        private JTextField text1;
        private JTextField text2;
        private JLabel label1;
        private JLabel label2;
        private JButton button1;
        private JButton button2;
     
        //constructors
     
        //other methods
    }

    While working on this project, I have found myself doing the following instead:

    public class Foo
    {
        public Foo( )
        {
             JTextArea = new JTextArea( );
             //so on and so forth for every object needed...
         }
     
         //other methods
    }

    I have seen it in some places where, all the variables being private, they are placed at the bottom of the code:

    public class Foo
    {
        //constructors
        //other methods
     
        private JTextField;
        private JTextArea;
        private JButton;
        //so on and so on... 
    }

    Which way do you use and what pro's and con's are there to each method?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Variable declaration placement

    Myself, I usually place GUI instance variables at the bottom, as there can be so many of them, which would push code too far down for my preference.

    I believe, cannot be 100% sure, that it is all up to your personal style/preference.
    Placing at the bottom I suppose puts the interface as the main focus. I usually always declare my instance variables up-top for non-GUI programs, or for very small GUI programs, though.

    (This is all from making Toy-Programs though, can't speak on a professional (or accurate?) level.)
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,103
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Variable declaration placement

    Many years ago sun/oracle laid out code conventions for java (see Code conventions) which state that instance variables be at the top (above the constructor) in the order of public, protected, private (see Class Declarations). You may choose to follow or not follow the rules, but even for things like posting code snippets on forums such as this, code that don't follow the rules may not help your chance at receiving replies. Many a time I have had to refactor code that doesn't follow the convention, and it is a royal pain in the you know what.

Similar Threads

  1. Placement of code within an application
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 12th, 2010, 07:26 PM
  2. Use variable in array declaration statement
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 24th, 2010, 03:42 PM
  3. Overriding methods declaration problems
    By TurboTuring in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 9th, 2010, 11:23 AM
  4. Very beginner - What's wrong in my applet declaration?
    By rforte in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 30th, 2010, 04:54 AM
  5. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 06:28 AM