Results 1 to 5 of 5

Thread: Constructor in Java

  1. #1
    Join Date
    Jan 2009
    Posts
    57

    Constructor in Java

    hi there

    Please help me in understanding a java concept.
    I am unable to understand a concept regarding constructors in java. As i am not so good in java programming language, please help me in understanding this concept of java

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Constructor in Java

    A java constructor has the same name as the name of the class to which it belongs. Constructor’s syntax does not include a return type, since constructors never return a value.

    Constructors may include parameters of various types. When the constructor is invoked using the new operator, the types must match those that are specified in the constructor definition.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Constructor in Java

    Java provides a default constructor which takes no arguments and performs no special actions or initializations, when no explicit constructors are provided.

    The only action taken by the implicit default constructor is to call the superclass constructor using the super() call. Constructor arguments provide you with a way to provide parameters for the initialization of an object.

    Below is an example of a cube class containing 2 constructors. (one default and one parameterized constructor).

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Constructor in Java

    public class Cube1 {

    int length;
    int breadth;
    int height;
    public int getVolume() {
    return (length * breadth * height);
    }
    Cube1() {
    length = 10;
    breadth = 10;
    height = 10;
    }
    Cube1(int l, int b, int h) {
    length = l;
    breadth = b;
    height = h;
    }
    public static void main(String[] args) {
    Cube1 cubeObj1, cubeObj2;
    cubeObj1 = new Cube1();
    cubeObj2 = new Cube1(10, 20, 30);



    System.out.println(”Volume of Cube1 is : ” + cubeObj1.getVolume());
    System.out.println(”Volume of Cube1 is : ” + cubeObj2.getVolume());
    }
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Constructor in Java

    A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that they use the name of the class and have no return type. For example, Bicycle has one constructor:
    public Bicycle(int startCadence, int startSpeed, int startGear) {
    gear = startGear;
    cadence = startCadence;
    speed = startSpeed;
    }
    To create a new Bicycle object called myBike, a constructor is called by the new operator:
    Bicycle myBike = new Bicycle(30, 0, 8);
    new Bicycle(30, 0, 8) creates space in memory for the object and initializes its fields.

    Although Bicycle only has one constructor, it could have others, including a no-argument constructor:
    public Bicycle() {
    gear = 1;
    cadence = 10;
    speed = 0;
    }

    Bicycle yourBike = new Bicycle(); invokes the no-argument constructor to create a new Bicycle object called yourBike.

Similar Threads

  1. What is constructor overloading in c++
    By Mast Maula in forum Software Development
    Replies: 4
    Last Post: 08-01-2011, 10:34 AM
  2. copy constructor Vs constructor
    By Truster in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 02:49 PM
  3. Selecting a constructor to use with new in C++
    By Carlton in forum Software Development
    Replies: 5
    Last Post: 02-02-2010, 12:30 AM
  4. Constructor vs Method
    By Taylor D in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 01:22 PM
  5. Frame constructor in java
    By HardWeaR in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 02:46 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,334,240.63788 seconds with 16 queries