+ 2

Can we pass argument to constructors in java ??

How to pass arguments to constructor in java ??

31st Oct 2017, 8:50 AM
Rishabh
Rishabh - avatar
4 Antworten
+ 10
ConstructorName (int arg){ doSomething (); } ___________ ConstructorName foo = new ConstruktorName (25);
31st Oct 2017, 8:57 AM
Kamil
Kamil - avatar
+ 8
It is simmilar to other methods. For example assume you have a class named Student which has a member variable age. Define a constructor as follows: public Student(int age){this.age=age;} Now defining an object of this class you can set its age: Student st= new Student(20); Then st.age equals 20.
31st Oct 2017, 9:02 AM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
+ 5
you should define it in its class.
31st Oct 2017, 9:17 AM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
+ 2
OK THANKS I WILL TRY TO DO THIS !! WAIT !
31st Oct 2017, 9:00 AM
Rishabh
Rishabh - avatar