Constructor in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Constructor in java

Plz,any body give me an example of constructor programs. plz,,plz,,

14th Jun 2017, 9:16 AM
Md king
Md king - avatar
6 Answers
+ 2
you ask why is there "this." before variable name? Yeah we know that we mean "assign age argument variable to class field called age" but compiler doesn't know that, so we have to point that "this is the one field from the class" by using "this" keyword. Hence this.age = age;
14th Jun 2017, 12:01 PM
Jakub Stasiak
Jakub Stasiak - avatar
+ 3
There is no such thing as "constructor program", the constructor is special method (function in class) which name has to be the same as name of class that is activated once you create instance of it eg: class Person{ private int age; public Person(int age) { this.age = age; } }; /////somewhere in main Person p(20); // We've activated constructor and we passed value "20" as a parameter of "age". Hope everything is clear, if any questions come to your mind just ask :)
14th Jun 2017, 9:21 AM
Jakub Stasiak
Jakub Stasiak - avatar
+ 3
thank you but√√√ Why we use this.age or this.a,but I know that in constructor the class name is same as we use in function. I want to say that I have no basically knowledge about java programs, I am just training of java,so if you have time then,you can understand me in detail if possible hence,no any away..
14th Jun 2017, 10:01 AM
Md king
Md king - avatar
+ 3
very useful topic, you may learn it properly... very helpful to program during object creation
14th Jun 2017, 6:22 PM
meherDev
meherDev - avatar
+ 3
Bhaijaan please visit durgasoft by ratan videos on YouTube. very great faculties there. For java Mr ratan is best.
14th Jun 2017, 6:25 PM
meherDev
meherDev - avatar
+ 2
class example { public int a; public example (int a){ this. a=a; } }
14th Jun 2017, 9:37 AM
Mohammed Ali Mujalli
Mohammed Ali Mujalli - avatar