+ 24
Why constructors are used in java?
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
+ 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
+ 22
to build an instance of the defined class (initiate the data members and call the constructors of the parents if it exists)
+ 9
To instantiate or create objects of a class. A class is just a blueprint for creating objects.
+ 7
Jasmine
Is methods and constructors both are same?
If yes then why to use constructors if we can do same thing by methods?
+ 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.
+ 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..
+ 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.
+ 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.
+ 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.
+ 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.
+ 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
+ 1
Constructors are used, because it initilizes each object of the class at the time of its creation.....