Java Constructor Parameters
In this chapter you will learn:
- How to pass in value with parameters for constructors
- Syntax for Java Constructor Parameters
- Example - Java Constructor Parameters
- How to declare object parameters for constructors
Description
The constructors can also have parameters. Usually the parameters are used to set the initial states of the object.
Syntax
Syntax for Java Constructor Parameters
class ClassName{//ww w . j a v a2 s . c o m
ClassName(parameterType variable,parameterType2 variable2,...){ // constructor
...
}
}
Example
In the the following demo code Rectangle
class uses
the parameters, w
for width and h
for height,
from the constructors to initialize
its width and height.
class Rectangle {
double width;/*from ww w .j a va 2 s . c o m*/
double height;
Rectangle(double w, double h) {
width = w;
height = h;
}
double area() {
return width * height;
}
}
public class Main {
public static void main(String args[]) {
Rectangle mybox1 = new Rectangle(10, 20);
double area;
area = mybox1.area();
System.out.println("Area is " + area);
}
}
The output from this program is shown here:
Example 2
Just like methods in a class the constructors can not only accept primitive type parameters it can also have the object parameters. Object parameters contains more information and can help us initialize the class.
The following Rectangle
class has a constructor whose parameter is a Rectangle
class.
In this way we can initialize a rectangle by the data from another rectangle.
class Rectangle {
double width;/*w w w.j a v a 2s . co m*/
double height;
Rectangle(Rectangle ob) { // pass object to constructor
width = ob.width;
height = ob.height;
}
Rectangle(double w, double h) {
width = w;
height = h;
}
// constructor used when no dimensions specified
Rectangle() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
}
// constructor used when cube is created
Rectangle(double len) {
width = height = len;
}
double area() {
return width * height;
}
}
public class Main {
public static void main(String args[]) {
Rectangle mybox1 = new Rectangle(10, 20);
Rectangle myclone = new Rectangle(mybox1);
double area;
// get volume of first box
area = mybox1.area();
System.out.println("Area of mybox1 is " + area);
// get volume of clone
area = myclone.area();
System.out.println("Area of clone is " + area);
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- What is Java this keyword for
- How to use this keyword to reference class member variables
- How to reference class member variables
- Example - use this to reference instance variable
Java Object
Java Object Reference Variable
Java Methods
Java Method Return
Java Method Parameters
Java Class Constructors
Java Default Constructor
Java Constructor Parameters
Java this KeywordJava static keyword
Java Method Overload
Java Constructors Overload
Java Method Argument Passing
Java Method Recursion
Java Nested Class
Java Anonymous Classes
Java Local Classes
Java Member Classes
Java Static Member Classes
Java Class Variables
Java main() Method
Java Class Inheritance
Java super keyword
Java Method Overriding
Java Constructor in hierarchy
Polymorphism
Java final keyword
Java Abstract class
Java Class Access Control
Java Package
Java Packages Import
Java Interface
Java Interface as data type
Java interface as build block
Java instanceof operator
Java Source Files