Results 1 to 8 of 8
  1. #1
    Java Riser's Avatar
    Java Riser is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default 2 Constructor questions

    Hello all,

    I'm very new to Java and have 2 questions about the code below. I was unable to properly code a solution (mine would compile and run but would display nothing at all) so I looked up the solution files to understand my mistake. I think I do, but I have 2 questions. The questions are after the code.

    This is the non-interesting file, I include it just to make sense of the next part, but I am curious about the red part:
    "
    Java Code:
    "
    public class Shirt {
    
    public int shirtID = 0;
    public String description = "-description required-";
    
    // The color codes are R=Red, B=Blue, G=Green, U=Unset
     public char colorCode = 'U';
    public double price = 0.0;
    public int quantityInStock = 0;
    
    [COLOR="Red"]public Shirt() {}[/COLOR]
    
    public Shirt(int ID, String d, char c, double p, int q) {
    
    shirtID = ID;
    description = d;
    colorCode = c;
    price = p;
    quantityInStock = q;}
    
      
    // This method displays the values for an item
    public void displayInformation() {
        System.out.println("Shirt ID: " + shirtID);
        System.out.println("Shirt description:" + description);
        System.out.println("Color Code: " + colorCode);
        System.out.println("Shirt price: " + price);
        System.out.println("Quantity in stock: " + quantityInStock);    }}
    "
    "
    First Question: -The Red Code- That indicates a constructor, does it not? It has the same name as the class, therefore making it a constructor, correct? Then why? Why have it there at all? It opens and closes "{}" immediately, why do that? then right after it it seems to have a second Constructor, overloaded. This second Constructor actually has a purpose, but why have the red one?:confused:

    and this is the interesting part"
    "
    Java Code:
    "
    public class ShirtArrayTest {
    public static void main (String args[]) {
    
    Shirt [] shirts;
    shirts = new Shirt[3];
    
    shirts[0] = new Shirt(44229, "Work", 'G', 29.99, 100);
    shirts[1] = new Shirt(33429, "Denim", 'R',44.99, 10);
    shirts[2] = new Shirt(43300, "Mesh", 'B', 79.99, 50);
    
    Shirt firstShirt = shirts[0];
    firstShirt.displayInformation();
    Shirt secondShirt = shirts[1];
    secondShirt.displayInformation();
    
    [COLOR="Magenta"]shirts[2].displayInformation();[/COLOR]    }}
    "
    "

    Question 2 -magenta-: :eek:You can do that?? It runs fine, but I'm aghast that you can just say it like that. Is it because it's the last one so by process of elimination the compiler knows it's the same as saying
    "Shirt thirdShirt=shirts[2];"
    "thirdShirt.displayInformation();"?
    Or did the programmer just not care about establishing in code that the [2] value was the third shirt as long as it displays correctly in this training exercise?

    Thanks a lot for your help in what must be basic to you.

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    For the first question, look at this: Java: Constructors
    It explains what happens if you don't give a constructor, it makes the object with initialized var. In your program, I think the exact same thing happens, and it is unnecessary as the compiler would have created it anyway.

    For the second question. An array is just storage for data (including objects) if you have an array of objects, you can take just one of those objects (objArray[2]) and run a method from it no problem, because you are just accessing the object in that array slot.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Java Riser View Post
    Hello all,
    and welcome to the forum.

    I'm very new to Java and have 2 questions about the code below. I was unable to properly code a solution (mine would compile and run but would display nothing at all) so I looked up the solution files to understand my mistake. I think I do, but I have 2 questions. The questions are after the code.
    First Question: -The Red Code- That indicates a constructor, does it not? It has the same name as the class, therefore making it a constructor, correct? Then why? Why have it there at all? It opens and closes "{}" immediately, why do that? then right after it it seems to have a second Constructor, overloaded. This second Constructor actually has a purpose, but why have the red one?:confused:
    The first constructor is the default constructor and it is in fact needed if your class has a non-default constructor (a constructor with parameters -- your second constructor there), and if someone using the class wants to ever use a default constructor at some time. A decent tutorial will likely explain this better than I just tried.

  4. #4
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    Quick question Fubarable. I understand that it would be important to use a normal constructor if you wanted one with parameters to be default. But I thought you would say:
    Java Code:
    public Constructor()
    {
    this(1, "string");
    }
    What use is making the constructor do nothing?
    Won't the compiler aways make a constructor that requires no parameters if the user didn't already make one? Or will it only make one if there is no constructor that has parameters.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by collin389 View Post
    Won't the compiler aways make a constructor that requires no parameters if the user didn't already make one? Or will it only make one if there is no constructor that has parameters.
    It will if there is no non-default constructor, but the minute that there exists a non-default constructor with arguments, you cannot call the default constructor unless one has been explicitly created first.
    Last edited by Fubarable; 02-20-2010 at 08:15 AM.

  6. #6
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    OK, thanks for the explaination.

  7. #7
    Java Riser's Avatar
    Java Riser is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    and welcome to the forum.

    Thanks, and thank you and Collin for the clear answer.

    I understand now.

    :)

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Java Riser View Post
    Thanks, and thank you and Collin for the clear answer.
    I understand now.
    :)
    You're quite welcome!

Similar Threads

  1. Some Questions
    By MuslimCoder in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 05:01 PM
  2. questions for 1yr exp
    By rahaman.athiq in forum Java Servlet
    Replies: 2
    Last Post: 11-26-2008, 02:13 AM
  3. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 10:01 AM
  5. 3 Questions
    By hiranya in forum AWT / Swing
    Replies: 4
    Last Post: 11-14-2007, 05:57 AM

Tags for this Thread

Posting Permissions

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