Why constructors are used in java?

5/14/2019 11:16:20 PM

Yamin Mansuri

12 Answers

New Answer


Yamin Mansuri 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 https://www.tutorialspoint.com/Difference-between-constructor-and-method-in-Java


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


to build an instance of the defined class (initiate the data members and call the constructors of the parents if it exists)


To instantiate or create objects of a class. A class is just a blueprint for creating objects.


Jasmine Is methods and constructors both are same? If yes then why to use constructors if we can do same thing by methods?


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. class Student { String name; int roll; Student(String n,int r)//Constructor { this.name=n; this.roll=r; } } public class Main { ....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.


hello Dear, 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.


Constructors are special member functions of a class. They are used to initialize the objects and its instance methods. If we do not make any constructor in our class then one default will be created.


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