Results 1 to 5 of 5
  1. #1
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default Why declaring Swing variables in longform

    I initially learnt how to design GUIs directly as source code. Now we (I am a student) are supposed to start using an interface to speed up the process. However, when I go into the source code I notice that the tool is generating longer code that I would have by hand. (I am using Netbeans)


    For example it generates this:

    Java Code:
    private javax.swing.JTextField topFieldText;

    Whereas I would ordinarily just do this:

    Java Code:
    private JTextField topFieldText;

    I know this is probably a nuisance question but I would appreciate a response that explains why it does this. Which one of the two is best practice?

    Many thanks in advance to those who will respond.
    Last edited by africanhacker; 02-06-2011 at 08:45 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Personally I wouldn't use the long winded form which I regard as far less readable. Sometimes the uneditable code in NB doesn't give you any choice in the matter.

    If you post code here, many will not bother to read screenfuls of NB generated code and will expect you to construct a small example by hand (the way you are used to).

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Note that NetBeans can't safely generate the short form of those class names because the user might have used those class names for his/her own program; two different classes with the same (unqualified) name yields uncompilable code.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Thanks guys, very speedy response. I would imagine that the clever people at Sun would have created an option for you to drop this needless code and take the risk of clashing classes, given that one would obviously be conscious of this possibility.

    All the same, thank you for the explanation.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by africanhacker View Post
    Thanks guys, very speedy response. I would imagine that the clever people at Sun would have created an option for you to drop this needless code and take the risk of clashing classes, given that one would obviously be conscious of this possibility.

    All the same, thank you for the explanation.
    NetBeans doesn't really look at your own code (it doesn't need to be finished yet) and if it generated short names for the Swing classes it blocks you in the near future from using those short names for other classes ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  2. Declaring variables
    By Flashmatix in forum New To Java
    Replies: 12
    Last Post: 01-20-2009, 03:43 PM
  3. Declaring a Queue
    By rhm54 in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 05:02 AM
  4. Declaring an ArrayList
    By bugger in forum New To Java
    Replies: 3
    Last Post: 01-31-2008, 07:36 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
  •