answersLogoWhite

0


Best Answer

A constructor is what allocates memory for an object. If you didn't call a constructor, you would have no object in which to assign values.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why there is the need of the constructor if you can assign the value to variables by the help of the methods ..?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the difference between the constructor to and destructor?

Functions and Constructors are similar in many ways. They can have arguments, they can have any amount of code, they can access the class's variables etc. the only difference is that a method in java needs to mandatorily have a return type but a Constructor in java cannot have a return type. It always creates and returns an object of the class for which it is the constructor. You cannot return a value from a constructor explicitly and if you try to do that, the compiler will give an error. The system knows that the purpose of the constructor is to create an object of the class and it will do the same irrespective of whether you declare a return type or not.


What value is stored in uninitialized variables?

Some languages assign a default value as 0 to uninitialized variables. In many languages, however, uninitialized variables hold unpredictable values.


Simple non-array variables are usually passed to methods by?

Simple non-array variables are usually passed to methods by value.


How can assign a value to a variable in GB Basic?

to simply assign a variable a numeric value a=10 anything=12 etc for string variables a$="sweet" ex$="happy christmas" etc to assign a value by input input"enter numerical value";numeric input"enter string value";string$ i hope that clears it up


What methods are used to place data in variables?

I will give examples in the Java language.Assign a value to a variable, with the equal sign. For example, if you want the variable "a" to have the value 5:int a;a = 5;Note that the first sentence declares the variable, this has to be done only once. After that, you can assign values to the variable several times; each value you assign will erase the previous value.The declaration and the assignment can be combined, thus:int a = 5;


Why you use constructor chaining?

Constructor ChainingWe know that constructors are invoked at runtime when you say new on some class type as follows:Lamborghini h = new Lamborghini();But what really happens when you say new Lamborghini() ? (Assume Lamborghini extends Car and Car extends Object.)1. Lamborghini constructor is invoked. Every constructor invokes the constructor of its superclass with an (implicit) call to super(),2. Car constructor is invoked (Car is the superclass of Lamborghini).3. Object constructor is invoked (Object is the ultimate superclass of all classes, so class Car extends Object even though you don't actually type "extends Object" into the Car class declaration. It's implicit.) At this point we're on the top of the hierarchy.4. Object instance variables are given their explicit values. By explicit values, we mean values that are assigned at the time the variables are declared, like "int x = 27", where "27" is the explicit value (as opposed to the default value) of the instance variable.5. Object constructor completes.6. Car instance variables are given their explicit values (if any).7. Car constructor completes.8. Lamborghini instance variables are given their explicit values (if any).9. Lamborghini constructor completes.


What is a plus c?

That depends how much "a" is, and how much "c" is. You can basically assign any value to these variables (letters); then add them up to get "a + c".


How would you find out the no of instance of a class?

Use a static variable in the class, initialised with the value zero. Each time any constructor is invoked, increment the variable and assign it to an instance member variable. The only exception is the move constructor which should take on the identity of its argument. class foo { private: static size_t counter; size_t identity; public: foo (): identity (++counter) {} // default constructor (new identity) foo (const foo& rhs): identity (++counter) {} // copy constructor (new identity) foo (foo&& rhs): identity (std::move(rhs.identity)) {} // move constructor (take identity from original) foo& operator= (const foo& rhs) {} // copy assign (retain current identity) foo& operator= (foo&& rhs) {} // move assign (retain current identity) ~foo() {} const size_t get_identity() const { return identity; } // accessor to identity }; // all static variables must be initialised outside of the class in which they are declared size_t foo::counter = 0;


Do variables that are declared but not initialised contain garbage values?

It depends on the language. In C and C++, all static variables are zero-initialised at runtime but local variables are not, thus an uninitialised local variable will hold whatever value happens to reside at the memory allocated to the variable at runtime. However, the C/C++ compiler can be configured to warn against using an uninitialised variable. In object oriented languages like Java there's no such thing as an uninitialised variable. This is because all Java variables are objects so we must pass the initial value to the object's constructor unless the object has default constructor. Attempting to default construct an object that has no default constructor is a syntax error.


What is the term used for undefined terms?

We often call "undefined terms" variables. Their value is not known; it can vary. And thus the name we assign them is variable.


True or False A C plus plus class constructor cannot return a function value?

True - A C++ constructor cannot return a value.


What is meant by a class in java programming?

Class - A class can be defined as a template/ blue print that describe the behaviors/states that object of its type support.Classes in Java:A class is a blue print from which individual objects are created.A sample of a class is given below: public class Dog{ String breed; int age; String color; void barking(){ } void hungry(){ } void sleeping(){ } }A class can contain any of the following variable types.Local variables . variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.Instance variables . Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.Class variables . Class variables are variables declared with in a class, outside any method, with the static keyword.A class can have any number of methods to access the value of various kind of methods. In the above example, barking(), hungry() and sleeping() are variables.