When the constructor taking parameters is known as n-arg or parameterized constructor.
Here in this context, instance variables equals local variables. Let’s see an example;
ConstructorExample2.java
class A { int x,y; A(int p,int q) //parametrized constructor { x=p; y=q; } void show() { System.out.println("x="+x); System.out.println("y="+y); } } class ConstructorExample2 { public static void main(String[] args) { A obj=new A(300,400); obj.show(); } }
Output:
Here in this example x, y are instance variables and p, q are local variables. According to the concept x=p and y=q. We know that when an object is created, the constructor execute automatically. Here at the time of declaration of object obj we have passed value 300 and 400. So 300 will assign to p and 400 will assign to q then the p value will assign to x and q value will assign to y. Then finally x=300 and y=400 as output.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc