+ 1

Why are constructors needed in java when we have methods..I see the advantage that we don't need to evoke it, but still why?desc

And can you also tell me about "this"?

21st Mar 2020, 8:55 PM
chaitanya mahaprabhu
chaitanya mahaprabhu - avatar
18 Antworten
+ 10
Constructors are needed to initialize objects
21st Mar 2020, 11:02 PM
JOY
JOY - avatar
+ 6
Here is whst I found related to constructors in java. Hope this helps to answer your question. Following are the difference between constructor and method. Constructor is used to initialize an object whereas method is used to exhibits functionality of an object. Constructors are invoked implicitly whereas methods are invoked explicitly. Constructor does not return any value where the method may/may not return a value. In case constructor is not present, a default constructor is provided by java compiler. In the case of a method, no default method is provided. Constructor should be of the same name as that of class. Method name should not be of the same name as that of class. reference: https://www.tutorialspoint.com/Difference-between-constructor-and-method-in-Java
21st Mar 2020, 9:25 PM
BroFar
BroFar - avatar
+ 4
no s= Student(1,100) is assigning Student(1,100) to s where as s.Student(1,100) is different
21st Mar 2020, 10:08 PM
BroFar
BroFar - avatar
+ 2
Good question Chaitanya mahaprabhu, Think of a Box. If we talk about a box class then it will have some class variables (say length, breadth, and height). But when it comes to creating its object(i.e Box will now exist in computer’s memory), then can a box be there with no value defined for its dimensions. The answer is no. So constructors are used to assign values to the class variables at the time of object creation, either explicitly done by the programmer or by Java itself (default constructor).
22nd Mar 2020, 10:58 AM
Sunny Akhil
Sunny Akhil - avatar
+ 2
Test
23rd Mar 2020, 3:23 PM
DebugTest
DebugTest - avatar
+ 1
BroFar I got it now... thank you! Jayakrishna 😬 I am still not able to figure out why we need constructors when we can assign default value to variables using methods itself.... but I am sort of getting sense that constructors initiate some process before program proceeds forward....is that true?
21st Mar 2020, 10:11 PM
chaitanya mahaprabhu
chaitanya mahaprabhu - avatar
0
Thank you for taking your time to answer @BroFar but when should we use it exactly?.....I am just confused with its purpose and usage
21st Mar 2020, 9:30 PM
chaitanya mahaprabhu
chaitanya mahaprabhu - avatar
0
chaitanya mahaprabhu Third para tells usage.. When you need to initialize some default values for an object data or properties, constructors are useful. When start writing code, you understand it more clearly...! Edit: class Student{ public int id, marks; Student() { this.id=0; this.id=0; } Student(int Id, int marks) { this.id=Id; this.marks=marks; } } Student s=new Student(); System.out.println(s.id); s=new Student(1,100); System.out.println(s.id); First constructor set default values. Second set input values... This way you can use it
21st Mar 2020, 9:41 PM
Jayakrishna 🇮🇳
0
Jayakrishna class X { X(int a) { System.out.print(a); } void random(int b) { System.out.print(b); } } When the function is called, won't it do the same thing as the constructor?.....I am sorry if I am being naive....I am just confused
21st Mar 2020, 9:47 PM
chaitanya mahaprabhu
chaitanya mahaprabhu - avatar
0
chaitanya mahaprabhu I already updated with an example above. For your example... You need to create object as X obj=new X(2); With this Contructor X(2) automatically gets executed and prints a vlaue... But to call random function you need this statement obj.random(2);
21st Mar 2020, 9:52 PM
Jayakrishna 🇮🇳
0
Jayakrishna Umm..... soooo the only difference is the calling part?....to avoid the method call we can use constructor?.... And in your updated answer, Is "s=Student(1,100)" same as s.Student(1,100)?
21st Mar 2020, 9:56 PM
chaitanya mahaprabhu
chaitanya mahaprabhu - avatar
0
No you get error for calling like that... See this same but different purpose constructor, Student() { this.id=10; this.id=100; } With privious What differences you find in this..? It setting 10,100 as default values.. You can set some output statements also for know a process started... Edit: Its S = new Student(1,100); I missed new there..
21st Mar 2020, 10:06 PM
Jayakrishna 🇮🇳
0
But we can do that with method too right?
21st Mar 2020, 10:08 PM
chaitanya mahaprabhu
chaitanya mahaprabhu - avatar
0
Sunny Akhil Thank you for the answer!....
22nd Mar 2020, 11:53 AM
chaitanya mahaprabhu
chaitanya mahaprabhu - avatar
0
chaitanya mahaprabhu In some scenarios, it's not recommended to use methods for assigning default values. Ofcouse you can but for that you need first an object. You need to call that method through that object. It's about 2 steps that too consecutive. If order missed, result will be undefined behaviour.. In thread environment you can't predict which one executes first.. It's depends on processor availability. in that case results may weird without constructors usage. Conbining those two steps, is we can say a Contructor. Without object creation you can't call non-static methods. [Ofcouse you can set default values by static variables but that sets 0 (for Integer) or null for strings. But you cant access null values.] Simply its better to use constructors instead of methods for setting default values.. Objects creation allocates memory for object properties(data) on which any modification done by the methods.. Methods can't able to do that. Java is object oriented programming , not functional programming..
22nd Mar 2020, 12:24 PM
Jayakrishna 🇮🇳
0
Cont. Everything is an object in java(almost). You can understand better if you know oops concepts and its advantages compare to other procedures. These all in my understandings in my words. May be there others points which Iam missing. I hope this only give some way of understanding process or give you an idea how it works only...
22nd Mar 2020, 12:39 PM
Jayakrishna 🇮🇳
0
Jayakrishna I got it now....thanks a lot!!
22nd Mar 2020, 1:10 PM
chaitanya mahaprabhu
chaitanya mahaprabhu - avatar
0
chaitanya mahaprabhu You're Wel come..
22nd Mar 2020, 1:13 PM
Jayakrishna 🇮🇳