Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 06-03-2009
Member
 
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
Reply With Quote
  #2  
Old 06-03-2009
Member
 
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.
Reply With Quote
  #3  
Old 06-03-2009
Member
 
Join Date: May 2008
Posts: 2,008
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).
Reply With Quote
  #4  
Old 06-03-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
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());
}
}
Reply With Quote
  #5  
Old 06-03-2009
Member
 
Join Date: May 2008
Posts: 2,293
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Constructor in Java"
Thread Thread Starter Forum Replies Last Post
What is constructor overloading in c++ Mast Maula Software Development 4 08-01-2011 10:34 AM
copy constructor Vs constructor Truster Software Development 5 08-02-2010 02:49 PM
Selecting a constructor to use with new in C++ Carlton Software Development 5 02-02-2010 12:30 AM
Constructor vs Method Taylor D Software Development 5 21-01-2010 01:22 PM
Frame constructor in java HardWeaR Software Development 3 08-12-2009 02:46 PM


All times are GMT +5.5. The time now is 09:38 PM.