DEV Community

Keshav Kumar
Keshav Kumar

Posted on

Difference Between Methods and Constructors (Java).

When you study first time about constructor it almost feels like this is completely equal to Method but no its not. You might be thinking what is the need to use constructor, we use constructor to initialize some value. when we use 𝗽𝗿𝗶𝘃𝗮𝘁𝗲 𝗶𝗻𝘁 𝗮;
in the class the defalut value of a is 0. But lets say if we want the default value of '𝗮' should be 5 then we can achieve that thorugh costructors.

now you might be thinking how will we identify which one is Method and which one is constructor, for that you have to see the name of constructor or method if that is equal to the name of class then it is a constructor else its a method.

also constructor is used to give default value and method is used to do some kind of operations like add();

Oldest comments (0)