Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By ozzyman

Thread: Questions

  1. #1
    BenH is offline Member
    Join Date
    Dec 2011
    Rep Power

    Default Questions

    Hi there!

    I am having a hard time understanding my Java text book thus I came up with some questions.

    Question 1:

    What is a constructor? What does a constructor look like?

    Question 2:

    What is the difference between private and public? What is their purpose?

    Question 3:

    What is the purpose of using the keyword this?

    Please help me understand these concepts. Thanks!

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    London, UK
    Blog Entries
    Rep Power

    Default Re: Questions

    To create an object instance you should call its constructor.

    For example, lets create a class which will store Rectangles.

    Java Code:
    class Rectangle {
    int width;
    int height;
    int x;
    int y;
    //default no-argument constructor
    Rectangle() {
    // We can make as many as we need
    // This is also a constructor
    Rectangle(int w, int h) {
    // this is our third Constructor
    Rectangle(int w, int h, int x, int y) {
    this(w, h);
    this.x = x;
    this.y = y;
    You can have as many Constructors as you want provided they have different combinations of parameter types
    The constructor is used to initialise an object, so usually in the constructor method you would initialise all of your class fields.
    If you do not write any constructors in your class a no argument constructor will be made. The variables will not be initialised as it will be a blank method.
    The constructor must be the same name as the class name.
    Here is an example of how you would use this class by calling the constructor
    Java Code:
    Rectangle rect1; //rect1 = null
    rect1 = new Rectangle(); //rect1 with width 0 height 0 pos 0,0
    Rectangle rect2 = new Rectangle(5,10); //a Rectangle object with width 5 height 10 pos 0,0
    Rectangle rect3 = new Rectangle(4,3,50,100); // width 4 height 3 pos 50,100
    Here we have used all 3 constructors.
    In the 3rd constructor the 'this' keyword became helpful in two ways
    1. To re-use existing code: we called the 2nd constructor to do the same job
    2. To help distinguish between a class variable and a local variable:
    The constructor accepted variables x and y, the class variable x and y was made distinct by using this.x and this.y

    Hope this makes some things clearer for you
    Last edited by ozzyman; 12-31-2011 at 09:20 AM.
    sunde887 likes this.

  3. #3
    karthiga is offline Member
    Join Date
    Jan 2012
    Rep Power

    Default Re: Questions


    constructor nothing but class name and function name is same. when u create an object that time the function is atomatically called.

    class a
    int a, int b;
    long c;

    a()// constructor simply explained

    Private And Public:
    in private variable could not accessed by outside the class.
    but public variables accessed by outside the class

    This keyword:

    In constructor function used the same arguments within function using (this.)


    Rectangle(int w, int h)

    thats all..All The Best...

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Rep Power

    Default Re: Questions

    karthiga, You can use [code] [/code] blocks to show source in a cleaner manner!

Similar Threads

  1. I have 2 questions =P
    By santa in forum New To Java
    Replies: 4
    Last Post: 01-19-2011, 06:35 PM
  2. Questions
    By Csharks in forum New To Java
    Replies: 8
    Last Post: 01-19-2011, 05:11 AM
  3. Need help with some questions
    By El_Davidos in forum New To Java
    Replies: 5
    Last Post: 11-23-2010, 11:13 AM
  4. questions for 1yr exp
    By rahaman.athiq in forum Java Servlet
    Replies: 2
    Last Post: 11-26-2008, 02:13 AM
  5. Questions About JSP?
    By mtz1406 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-19-2008, 08:56 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