ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

What is the need of constructor in java?

user-image
تم إضافة السؤال من قبل Manish Gour
تاريخ النشر: 2013/12/22
سعد رضا ٰ
من قبل سعد رضا ٰ , Software Engnieering , None

Because we've made the field variables private, we need another way to assign values to them. One way to do this is with something called a constructor. This is a method you can use to set initial values for field variables. When the object is created, Java calls the constructor first. Any code you have in your constructor will then get executed. You don't need to make any special calls to a constructor method - they happen automatically when you create a new object.

Constructor methods take the same name as the class. Add the following constructor to your StudentResults class:

A Java class constructor has been added to the code

So the name of the constructor is StudentResults. This is exactly the same name as the class itself. Unlike normal methods, class constructors don't need a return type like int or double, nor any return value. You can, however, pass values over to your constructors. If we want to pass values over to our field variables, we can do this:

The class constructor setting values for field variables

Here, we've added two String variables to the round brackets of the constructor. Inside the curly brackets we've assigned these values to the Full_Name and Exam_Grade fields. When you create a new object, you'd then need two strings between the round brackets of the class name:

StudentResults aStudent = new StudentResults( "Bill Gates", "A" );

When the object is created, the values "Bill Gates" and "A" will be handed over to the constructor.

However, it's a good idea to just set some default values for your field variables. These values will then be assigned when the object is created. Add the following to your constructor:

Setting default values in the constructor

All four of our field variables will now have default values whenever a new StudentResults object is created. Notice that we now don't have anything between the round brackets of the class constructor.

In the next part, we'll take a look at accessing class variables.

 

Khalid Omar
من قبل Khalid Omar , Senior .NET Developer / Associate Technology Manager , Musafir.com (Universal Tourism)

You need a constructor simply to perform certain tasks when an object is instantiated with new. Think of a constructor as a member method having the same class name. The method is called automatically whenever you instantiate (create) an instance of your class.

 

Typically, a constructor is used to initialize class fields regardless of their access modifiers (private/protected/public).

 

Similar to methods, a class constructor can be overloaded by giving different parameters to each constructor. Check this: http://stackoverflow.com/a/2632401

مستخدم محذوف‎
من قبل مستخدم محذوف‎

Is Utilized to create a new Class instance ( Object) by reserving space for the Member Variables 

 

Is a function inside the class responsible for initialization

 

Is not a conventional method

 

Has the same name as the class

 

Invoked with new that creates the object

 

If no constructor is defined, it means the language provides no arg constructor that does nothing.

 

 

When you create an object you are actually calling its constructor.

 

Constructor usually starts with the ‘public’ keyword

 

It is followed by Name of the constructor

 

Constructor argument in parenthesis

 

Example: Call them using new operator

 

   

Rectangle rec= new FallingRectangle (RectangleSize)

 

Preetham D
من قبل Preetham D , Software Engineer / Senior Software Engineer , Mindtree

Whenever the object is instatiated, the data asscociated with the object of class can be intialized with the help of constructor.

Constructor name is same as Class number, but without any return type

المزيد من الأسئلة المماثلة