Results 1 to 7 of 7
Thread: No-arg constructor??
- 03-09-2011, 02:19 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
No-arg constructor??
I am little confused about no-arg consturctor and need help in understanding exactly what it is. I have googled this topic and is more confused. I have created a class Rectangle. One of my methods is called public double Rectangle(){} which I thought was a no arg constructor.
But when I compile I am getting an saying missing return statement line 9 (verbatim). Am I missing something?
Java Code:public class Rectangle { private double height = 1.0; private double width= 1.0; private static String color; public double Rectangle() {} public Rectangle (double high, double wid) { this.width=wid; this.height= high; } public Rectangle (String recColor) { color = recColor; } public void setHeight(double high) { this.height = high; } public void setWidth(double wid) { this.width = wid; } public void setColor(String col) { color = col; } public double getHeight() { return height; } public double getWidth() { return width; } public double getArea() { return height*width; } public double getPerimeter() { return 2*(height + width); } public String getRecColor() { return this.color; } }
- 03-09-2011, 02:23 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Constructors don't need a return type.
No arg constructors are the same as constructors that take arguments. They are used to be the default way to initialize some object.Last edited by sunde887; 03-09-2011 at 02:26 AM.
- 03-09-2011, 02:24 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
Are you trying to have a constructor which creates a rectangle, but takes no arguments? You are on the right track, remember a constructor can not return a value...
- 03-09-2011, 02:38 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
When I try to compile this code I am receiving an error "missing return statement - line 9 (verbatim). ". Line 9 is: Rectangle(){} . So I am not sure why it is telling me to put a return statement on a no arg constructor. Can someone help understand why I am receiving this error on a no arg constructor?
- 03-09-2011, 02:42 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Do the other constructors work? A constructor should not have a return type. Just a random attempt at a solution is adding the line this(); in the code block.
- 03-09-2011, 02:44 AM #6
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
This compiles for me:
Java Code:public class Rectangle { private double height = 1.0; private double width= 1.0; private static String color; public Rectangle() {} public Rectangle (double high, double wid) { this.width=wid; this.height= high; } public Rectangle (String recColor) { color = recColor; } public void setHeight(double high) { this.height = high; } public void setWidth(double wid) { this.width = wid; } public void setColor(String col) { color = col; } public double getHeight() { return height; } public double getWidth() { return width; } public double getArea() { return height*width; } public double getPerimeter() { return 2*(height + width); } public String getRecColor() { return this.color; } }
- 03-09-2011, 02:50 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
Ok, looking at it change the no arg constructor and it works better and no compile errors:
Java Code:public class Rectangle { //Declare variables private double height; private double width; private static String color; //no arg constructor public Rectangle() { height = 1.0; width= 1.0; color="Red"; }
Similar Threads
-
Constructor
By theoneroo in forum New To JavaReplies: 5Last Post: 01-28-2011, 09:37 AM -
Constructor
By Sarinam in forum AWT / SwingReplies: 1Last Post: 06-19-2008, 08:03 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 -
Constructor Help
By bluegreen7hi in forum New To JavaReplies: 2Last Post: 11-15-2007, 05:44 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks