Results 1 to 4 of 4

Thread: Constructors?

  1. #1
    annna is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default Constructors?

    I'm in the midst of my tutorial and am stuck at this qns.
    Below is the code,

    class UsePoint
    {
    public static void main(String[] args)
    {
    Point p=new Point();
    p.print();

    }
    }

    class Point
    {
    private int x;
    private int y;

    Point() (CONSTRUCTOR? WHAT THEY ACTUALLY DO)
    {
    this(0,0);
    }

    Point(int x,int y)

    {
    this.setPoint(x,y);
    }

    void setPoint(int x, int y)
    {
    this.setPoint(x,y);
    }

    void print()
    {
    System.out.println("Point: ");
    System.out.println(this.x);
    System.out.println(this.y);
    }

    }

    Before attempting the qns, I need some help on the parts of the code I don't understand (in a different color and underlined).
    Need help understanding them!Thanks!

  2. #2
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    please read this link. It was for me the best link. please read all pages of this link.
    Object Initialization in Java

  3. #3
    Boxxybabee's Avatar
    Boxxybabee is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default

    Okay I think I can help you with this.

    Java Code:
    public Point(int x, int y) {
    //sets private int x to int x in the constructor parameters. Then sets private int y to int y in the constructor parameters. So what ever you put in for x and y will be where the point is at.
    x=x;
    y=y;
    then you can make a scanner object that lets you input what ever you want for x and y. then you can use system.out to print the point.

    I hope this helped. I'm not quite sure if it did.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Boxxybabee View Post
    Okay I think I can help you with this.

    Java Code:
    public Point(int x, int y) {
    //sets private int x to int x in the constructor parameters. Then sets private int y to int y in the constructor parameters. So what ever you put in for x and y will be where the point is at.
    x=x;
    y=y;
    then you can make a scanner object that lets you input what ever you want for x and y. then you can use system.out to print the point.

    I hope this helped. I'm not quite sure if it did.
    Not quite. These statements in fact don't do anything:
    Java Code:
    x = x;
    y = y;
    Java thinks that you are trying to set a parameter = to itself, even if you have class fields named x, and y. The proper way to set class fields with the same name as method or constructor parameters is to use the "this" key word:
    Java Code:
    this.x = x;
    this.y = y;

Similar Threads

  1. constructors?
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 10-13-2009, 02:14 PM
  2. Constructors
    By new2java2009 in forum New To Java
    Replies: 5
    Last Post: 08-18-2009, 06:46 AM
  3. Need some help with a task, constructors.
    By JKM in forum New To Java
    Replies: 1
    Last Post: 02-01-2009, 10:09 PM
  4. Help with constructors
    By Minime in forum New To Java
    Replies: 3
    Last Post: 04-09-2008, 07:59 AM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 03:15 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
  •