No argument Constructors
Categories: Java 8(JDK1.8)
As the name specifies the no argument constructors of Java does not accept any parameters instead, using these constructors the instance variables of a method will be initialized with fixed values for all objects.
Example
Public class MyClass {
Int num;
MyClass() {
num = 100;
}
}
Parameterized Constructors
Most often, you will need a constructor that accepts one or more parameters. Parameters are added to a constructor in the same way that they are added to a method, just declare them inside the parentheses after the constructor's name.
Example
Here is a simple example that uses a constructor −
// A simple constructor.
class MyClass {
int x;
// Following is the constructor
MyClass(int i ) {
x = i;
}
}