answersLogoWhite

0


Best Answer

A constructor creates an Object of the class that it is in by initializing all the instance variables and creating a place in memory to hold the Object. It is always used with the keyword new and then the Class name. For instance, new String(); constructs a new String object. Sometimes in a few classes you may have to initialize a few of the variables to values apart from their predefined data type specific values. If java initializes variables it would default them to their variable type specific values. For example you may want to initialize an integer variable to 10 or 20 based on a certain condition, when your class is created. In such a case you cannot hard code the value during variable declaration. such kind of code can be placed inside the constructor so that the initialization would happen when the class is instantiated.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

A constructor, in object oriented programming concept is a method which has the same name of the class where it is defined. It will be called when an object is created for that class. The job of the constructor is the initialization of the members of the class.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

A constructor is a special class method that is invoked whenever an object of the class is instantiated. The constructor is used to initialise the object such that when construction is complete, a valid object exists. Constructors cannot be called directly since they are not functions (they have no return value, not even void), however constructors can accept arguments in the same way functions can. Constructors are invoked, rather than called.

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

A constructor is a special class member that is invoked every time an object is instantiated from a class. Constructors are specifically used to initialise the non-static data members of the class and to establish the class invariant.

A class may define as many constructors as required, however "ordinary" classes will define at least three constructors:

class X {

public:

X (int value=0): data {value} {} // default constructor

X (const X& other): data {other.data} {} // copy constructor

X (X&&): data {std::move {other.data} {} // move constructor

private:

int data;

// ...

};

Note that constructors differ from functions in that there is no return value (not even void) and they include a comma-separated list of list of member initialisers between the argument list and the constructor body. In most cases, the constructor body is empty, but we can use the body to perform additional initialisations that cannot easily be performed via member initialisers. For efficiency, we perform as much initialisation as possible before entering the constructor body.

A default constructor is a constructor that accepts no arguments or where all arguments have default values. Default constructors are useful for objects that have a natural default value or state. For instance, a container object will typically default to an empty container, a string object to an empty string and a numeric object to zero.

A copy constructor is a constructor that accepts an existing object of the same class and constructs a logical copy of that object. Given that the existing object's members will not be changed by making a copy, the argument is declared const. Copy constructors are invoked automatically whenever we pass an object to a function by value.

A move constructor is a constructor that accepts a modifiable rvalue. Unlike copy constructors, move constructors take ownership of the argument's resources and leave the moved-from object in an unspecified but valid state, the assumption being that the moved-from object will subsequently be destroyed. Move constructors are invoked automatically when returning an object by value.

Constructors are always invoked at the point an object of the class is declared (the point of instantiation). Unlike C where all variables are, by convention, explicitly declared at the top of the function, objects should only be instantiated when we have a suitable value to assign to them. The difference is best explained by example:

X x; // default constructed

// ...

x = 42; // assumes X::operator= (int) is defined

This is known as two-stage initialisation. Ideally, we would re-order the statements as follows:

// ...

X x {42};

This is more efficient. The more complex the object, the more efficient it becomes.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Every class, including abstract classes, MUST have a constructor. Hard Code that into your brain. But just because a class must have one, doesn't mean the programmer has to type it. A constructor looks like this:

class Car {

Car() { } // The constructor for the Car class

}

You notice anything missing in the declaration above? There's no return type! Two key points to remember about constructors are that they have no return type and their names must exactly match the class name. Typically, constructors are used to initialize instance variable state, as follows:

class Car {

int size;

String name;

Car(String name, int size) {

this.name = name;

this.size = size;

}

}

In the preceding code example, the Car class does not have a no-arg constructor. That means the following will fail to compile:

Car f = new Car(); // Won't compile, no matching constructor

but the following will compile:

Car f = new Car("Ford", 43); // No problem. Arguments match

// the Car constructor.

So it's very common for a class to have a no-arg constructor, regardless of how many other overloaded constructors are in the class (constructors can be overloaded just like methods). You can't always make that work for your classes; occasionally you have a class where it makes no sense to create an instance without supplying information to the constructor. A java.awt.Color object, for example, can't be created by calling a no-arg constructor, because that would be like saying to the JVM, "Make me a new Color object, and I really don't care what color it is...." Do you seriously want the JVM making your color choices?

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

It is a method called when the runtime constructs an instance of a Java class. You can either create your own constructor (or constructors, if you want to allow for parameterized construction) or, if you don't need any customized construction, you can use the default no-argument constructor which is inserted automatically thanks to the implicit extension of the Object-class.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Constructor is usually the first piece of code in a class that gets executed when you try to instantiate a class. As its name suggests, it constructs the object for the class.

In Java, objects are constructed. Every time you make a new object, at least one constructor is invoked. Every class has a constructor, although if you don't create one explicitly, the compiler will build one for you.

Ex:

class Test {

public Test() { } // this is Test's constructor

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

initialises the object and allocate memory space

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

It is the way to create the instances of class. in java. to know how constructor works first you should be clear with the concept of class.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is use of constructor in object oriented programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why is Java called an Object Oriented Programming Language?

Actually java is not purely object oriented.because we can use the primitive data types in Java.In java all those things or considered as classes and objects .So we are called java is an object oriented programming language...


Why do you need objects in Java programming?

Object-oriented programming provides several advantages, including code re-use and code robustness.


What is a constructor and its mandatory to use constructor in a class?

A constructor is a method that is invoked when an object is created. As to being mandatory, that really depends on the programming language; in the case of Java, each class must have a constructor, however, in many cases Java will automatically provide a default constructor, so you don't really need to program it.


What are multiple constructors in c?

A constructor is a special method that is created when the object is created or defined. This particular method holds the same name as that of the object and it initializes the instance of the object whenever that object is created. The constructor also usually holds the initializations of the different declared member variables of its object. Unlike some of the other methods, the constructor does not return a value, not even void.When you create an object, if you do not declare a constructor, the compiler would create one for your program; this is useful because it lets all other objects and functions of the program know that this object exists. This compiler created constructor is called the default constructor. If you want to declare your own constructor, simply add a method with the same name as the object in the public section of the object. When you declare an instance of an object, whether you use that object or not, a constructor for the object is created and signals itself.A constructor is declared without a return value, that also excludes void.Therefore, when implemented, do not return a value:Constructor Exampleclass rectangle { // A simple class int height; int width; public: rectangle(void); // with a constuctor, ~rectangle(void); // and a destructor }; rectangle::rectangle(void) // constuctor { height = 6; width = 6; } sagar sainath samant. sybsc (computer science)


What are the disadvantages of procedure oriented programming in c plus plus?

There are no any disadvantages of procedure oriented programming in C++. You can use it as well as object oriented programming, generic programming or any other paradigm. Just remember that this way you won't be using many helpful features of that language.

Related questions

Why did computer scientists introduce object - oriented programming?

If you work a while with object-oriented programming, you'll notice that it offers huge benefits over the traditional approach. In fact, you would rather not use a programming language that doesn't have at least the option of object-oriented programming, if you have the choice.


What are the companies which use pure object oriented programming languages?

Microsoft, Apple and so on.


Why is Java called an Object Oriented Programming Language?

Actually java is not purely object oriented.because we can use the primitive data types in Java.In java all those things or considered as classes and objects .So we are called java is an object oriented programming language...


Why do you need objects in Java programming?

Object-oriented programming provides several advantages, including code re-use and code robustness.


Thw importance of ASP.net?

Object Oriented Programming is a subset of structured programming. After objects are created in a program, you use those objects and their methods to operate the program. In structured programming, you have a program with many methods in which you can use. One difference between structured programming and object-oriented programming is that structured programming uses the data that is given to them through parameters, while in object-oriented programming, the methods act upon the object's data (fields). This makes programming much easier because the fields are all there and you do not have to make sure that the correct field is passed to the correct method. All you have to do is call which field you want to work with.


What is a constructor and its mandatory to use constructor in a class?

A constructor is a method that is invoked when an object is created. As to being mandatory, that really depends on the programming language; in the case of Java, each class must have a constructor, however, in many cases Java will automatically provide a default constructor, so you don't really need to program it.


What is use of constructor in java?

Constructor is used to do something (written in constructor) immediately after object creation.


What kind of written language did Delphi use?

Delphi used a programming language called Object Pascal for its written language. Object Pascal is an extension of the Pascal language that includes object-oriented programming features.


How can i use the word hierarchical in a sentence?

The hierarchical concepts are there in object oriented programming. It is the sentence containing the word hierarchical.


Why you use constructor overloading?

When we are initializing our object with different internal state then we can use the constructor overloading.


What is OOP language?

Before Object oriented programming the term which was used was “Structural Programming” where a programmer has to use the predefined data types, they cannot define their own data type. But when the term object oriented programming start in programming programmer become able to define its own data type according to their own use. Here a coder can define its own data types called classes and then create object from these classes. Coding in Object Oriented language Advantages of Object Oriented Programming One of the main advantages of OOP over procedural programming techniques is that they enable programmers to create modules that do not need to be changed when a new type of object is added. A programmer simply can create a new object that inherits from existing objects many of its features . This makes object-oriented programs easier for programmer to modify.


Use of constructor?

to create an instance of object