simple Java program using constructor
Here you will find a simple Java program that uses a constructor method in a class. Also the use of “this” is shown in the example. The use of new is also demonstrated to instantiate new instances of a class.
Java program – using constructor, “new” and “this”
package test;
class A
{
int i;
A(int i)
{
this.i=i;
}
void show()
{
System.out.println(“Value of id: “+i);
}
}
class B
{
public static void main(String args[])
{
A a1=new A(5);
A b1=new A(7);
a1.show();
b1.show();
}
}