Results 1 to 7 of 7
  1. #1
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default why ListName = new JList(arrayName)?

    so im learning java and can't figure why you do this(JList is just an example)
    Java Code:
    private JList listName;
    ListName = new JList(arrayName);
    instead of just
    Java Code:
    public JList ListName = arrayName;
    like you would with a normal integrer:
    Java Code:
    public int intName= 4;
    Thanks :D
    Last edited by Xyexs; 06-13-2013 at 03:44 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: why ListName = new JList(arrayName)?

    You could do it in one line:

    private JList listName = new JList(arrayName);

    But you can't do this:

    private JList listName = arrayName;

    Because arrayName is (presumably) an array. An array is not a JList. A JList can take an array as an argument into its constructor, but that doesn't mean it *is* an array.

    Recommended reading: Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: why ListName = new JList(arrayName)?

    In JAVA, you can declare and initialize most variables with separate statements.

    JAVA is case-sensitive, so when you say

    Java Code:
    private JList listName;
    ListName = new JList(arrayName);
    I'm assuming you mean
    Java Code:
    private JList listName; //declaring the JList
    listName = new JList(arrayName); //initializing the JList
    All the above code is doing is separating the declaration from the initialization.
    When the 1st line is executed, the JList called listName has a null value.
    After the 2nd line is exeducted, listName now references a JList object.

    You are correct, nothing prevents you from doing this in a single line, as you mentioned.

    Make sense?

  4. #4
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: why ListName = new JList(arrayName)?

    Quote Originally Posted by KevinWorkman View Post
    You could do it in one line:

    private JList listName = new JList(arrayName);

    But you can't do this:

    private JList listName = arrayName;

    Because arrayName is (presumably) an array. An array is not a JList. A JList can take an array as an argument into its constructor, but that doesn't mean it *is* an array.

    Recommended reading: Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Cool! just one more thing- why the keyword new?

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: why ListName = new JList(arrayName)?

    Quote Originally Posted by Xyexs View Post
    Cool! just one more thing- why the keyword new?
    Read the tutorial on constructors I posted, it explains the new keyword.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: why ListName = new JList(arrayName)?

    i see- thanks

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: why ListName = new JList(arrayName)?

    Of course it all depends where 'arrayName' comes from.
    If that's a parameter to the constructor (for example) then you could not initialise the 'listName' attribute at the same time as the declaration of it as 'arrayName' is not available at that point.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 4
    Last Post: 01-10-2012, 01:05 PM
  2. Link one JList to another JList
    By mib1bee in forum AWT / Swing
    Replies: 1
    Last Post: 12-31-2010, 07:10 PM
  3. Link one JList to another JList
    By mib1bee in forum Advanced Java
    Replies: 1
    Last Post: 12-30-2010, 06:35 PM
  4. Link one JList to another JList
    By mib1bee in forum New To Java
    Replies: 1
    Last Post: 12-30-2010, 06:24 PM
  5. About JList
    By hungleon88 in forum Advanced Java
    Replies: 5
    Last Post: 08-30-2008, 09:24 PM

Posting Permissions

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