No no I mn to say both r work like functions
And to invoke we should Hv to create object to both of them and
For constructor while creating object it will be invoked
But in methods we need to add some more info
This is the main difference
U can refer here
Both methods and constructors are just like functions
Here both will called by creating objects but constructor used for initiating members of a class and it can be called easily only by creating new instances
Java is a OOP (object oriented programming) language.. so you need to understand OOP paradigms.. one of them are constructors..
so basically OOP base on classes that are molds to create objects.. the way it's done is described in a special type of class methods called constructors..
Jasmine constructors are a special kind of methods..
Constructors are used in Java to pass any value or reference to the object's class during object creation.
Student(String n,int r)//Constructor
public class Main
Student s1=new Student("Sujal",5);
This code will create an object named 's1' and passes values such as name & roll no. to the referred class for further work during creating it.
Constructors are automatically called whenever an object is instantiated. Thereby your object's attributes would have default values and won't be undefined. A method can work but it needs to be called explicitly. The main task here is to initialize the members.
Constructors are used to instantiate an object at d exact moment it is created. Yes you can do the same using functions..by there r some situations in which constructor is a good alternative. Such as when creating roll no auto generation prgrm.
Constructor and method are same bur we need to call manually for function while constructor call it self.
For example Database connection is need for all crud operation so for that we can use constructor so no need to create connection every time or manually.
Every class has a constructor whether it’s a normal class or a abstract class.Constructors are not methods and they don’t have any return type.Constructor name should match with class name .Constructor can use any access specifier, they can be declared as private also. Private constructors are possible in java but there scope is within the class only.Like constructors method can also have name same as class name, but still they have return type, though which we can identify them that they are methods not constructors.If you don’t implement any constructor within the class, compiler will do it for.this() and super() should be the first statement in the constructor code. If you don’t mention them, compiler does it for you accordingly.Constructor overloading is possible but overriding is not possible. Which means we can have overloaded constructor in our class but we can’t override a constructor.Constructors can not be inherited.If Super class doesn’t have a no-arg(default) constructor then compiler would not in