DEV Community

Keshav Kumar
Keshav Kumar

Posted on

2

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();

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay