+ 24

Why constructors are used in java?

14th May 2019, 11:16 PM
Yamin Mansuri
Yamin Mansuri - avatar
13 Antworten
+ 28
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
15th May 2019, 8:26 AM
Jas
Jas - avatar
+ 24
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
15th May 2019, 12:45 AM
Jas
Jas - avatar
+ 22
to build an instance of the defined class (initiate the data members and call the constructors of the parents if it exists)
15th May 2019, 8:20 AM
Nour Abu Assaf
Nour Abu Assaf - avatar
+ 9
To instantiate or create objects of a class. A class is just a blueprint for creating objects.
14th May 2019, 11:55 PM
Sonic
Sonic - avatar
+ 7
Jasmine Is methods and constructors both are same? If yes then why to use constructors if we can do same thing by methods?
15th May 2019, 4:14 AM
Yamin Mansuri
Yamin Mansuri - avatar
+ 5
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.
15th May 2019, 7:19 AM
Aayush Bhansali
Aayush Bhansali - avatar
+ 5
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..
16th May 2019, 4:27 PM
Mia380
Mia380 - avatar
+ 4
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.
15th May 2019, 5:45 AM
Raman Singh
+ 4
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.
15th May 2019, 6:12 AM
Sujal Kumar
Sujal Kumar - avatar
+ 3
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.
16th May 2019, 1:13 PM
neha yadav
neha yadav - avatar
+ 2
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.
15th May 2019, 8:03 AM
Nirali Vachhani
Nirali Vachhani - avatar
+ 1
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
15th May 2019, 4:58 PM
Jaliya Roshen
Jaliya Roshen - avatar
+ 1
Constructors are used, because it initilizes each object of the class at the time of its creation.....
1st Jun 2019, 3:53 PM
@mÂĄÂŁ