answersLogoWhite

0

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

15y ago

What else can I help you with?

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 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.


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.

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 use of constructor in java?

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


What defines the behavior of an object in object-oriented programming and how is it implemented within the programming paradigm?

In object-oriented programming, the behavior of an object is defined by its methods, which are functions that describe what the object can do. This behavior is implemented by creating classes that define the structure and behavior of objects, and then creating instances of those classes to use in the program. The methods within the class determine how the object interacts with other objects and data in the program.


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.


Why you use constructor overloading?

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


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.


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