why do we need constructor in java
In this article we will discuss need and usage details of constructors in Java. What is a Constructor? Constructor is a member function of the class that is used to initialize a newly constructed object i. e. initializing the member variables of object. It has the same name as of the class. Why do we need Constructors? Whenever we create an object in Object Oriented Methodology, we first need initialize its member variables. For example, we have a class Game with name and level as its member variables. Now suppose we created an object of this class Game, but forgot to initialize it. In that case its name and levels for this object will be garbage and will give undesired result when we call other member functions that uses them. As initialization is must for an object before using it, therefore in Java language support for Constructors has been added.
Whenever we create a new object in Java, first of all its Constructor is called to initialize its value. For example, we have a class Game, lets create an object of it,
Before this new object is assigned to reference obj, its constructor will be called internally. Lets see the complete example, Output: 1. ) Default Constructor: Constructor that take zero argument is called Default Constructor. In the above example we have defined a default Constructor i. e. 2. ) Parameterized Constructors: Constructor that take arguments are called parameterized Constructors. i. e. Lets add this in to the above class i. e. Output: As Constructors are called internally at the time of object creation.
So, nobody knows what value to expect in return from a Constructor and what to do with it. Therefore Constructors don t return any value. Overloading a member function means defining different member functions with same but different argument list. Constructors can be overloaded too i. e. defining different parameterized constructors will result in constructor overloading. Lets add a overloaded constructor in above class i. e. Complete working code is as follows, 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: 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: 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: 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.
- Views: 98
why do objects fall at the same time
why do some objects sink while others float
why is all data stored in binary form
why do planets look brighter than stars
young living essential oils where to buy
why we use string args in main method in java
why men are not allowed to take messages