Results 1 to 8 of 8
Thread: 2 Constructor questions
- 02-20-2010, 06:31 AM #1
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.
- 02-20-2010, 06:42 AM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
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.
-
and welcome to the forum.
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.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:
- 02-20-2010, 07:01 AM #4
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
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:
What use is making the constructor do nothing?Java Code:public Constructor() { this(1, "string"); }
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.
-
- 02-20-2010, 07:22 AM #6
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
OK, thanks for the explaination.
- 02-22-2010, 12:31 AM #7
-
Similar Threads
-
Some Questions
By MuslimCoder in forum New To JavaReplies: 2Last Post: 02-25-2009, 04:01 PM -
questions for 1yr exp
By rahaman.athiq in forum Java ServletReplies: 2Last Post: 11-26-2008, 01:13 AM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM -
3 Questions
By hiranya in forum AWT / SwingReplies: 4Last Post: 11-14-2007, 04:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks