Results 1 to 2 of 2
  1. #1
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default Rectangle class with no argument constructor

    I need to write a rectangle class using a no argument constructor. What I have come up with that doesn't work....I am so lost right now... is:

    public class Rectangle {


    private int height=10;
    private int width=25;


    public Rectangle() {

    Rectangle rect = new Rectangle();

    getHeight(){
    return (height);
    }

    getWidth(){
    return (width);
    }

    }

    public void print(){
    System.out.println(height+width);


    I need to output the width and height as set as w=25 and h=10
    thank in advance

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The rectangle constructor should not create an instance of rectangle in the constructor. Think what would happen if this was possible? If the constructor uses the constructor to create an instance of the class you would get an infinite recursive type loop.

    Also, constructors don't return anything. All the constructor should do(for the most part) is set the classes instance variables.

Similar Threads

  1. Replies: 1
    Last Post: 02-07-2011, 10:44 AM
  2. What is the Use of No argument Constructor in JAVA Beans
    By JyotirmoyDeb in forum New To Java
    Replies: 2
    Last Post: 10-16-2010, 01:44 AM
  3. How to resolve Constructor argument in Spring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-29-2008, 12:43 PM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. How to resolve Constructor argument in Spring
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 08:34 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
  •