What is constructors and explain with an example, JAVA Programming

Assignment Help:

What is Constructors? Explain with an example?

A constructor forms a new instance of the class. It initializes all the variables and does any work essential to prepare the class to be used. In the line

Car c = new Car();
Car() is the constructor. A constructor has the similar name as the class.
If no constructor exists Java gives a generic one that takes no arguments (a noargs constructor), other than it's better to write your own. You make a constructor through writing a technique that has the same name as the class. Therefore the Car constructor is called Car().

Constructors do not have return types. They do return an instance of their own class, other than this is implicit, not explicit.
The subsequent method is a constructor that initializes license plate to an empty string, speed to zero, and maximum speed to 120.0.

Car() {
this.licensePlate = "";
this.speed = 0.0;
this.maxSpeed = 120.0;
}
Better yet, you can form a constructor which accepts three arguments and use those to initialize the fields as below.
Car(String licensePlate, double speed, double maxSpeed) {

this.licensePlate = licensePlate;
this.speed = speed;
if (maxSpeed > 0) this.maxSpeed = maxSpeed;
else this.maxSpeed = 0.0;
if (speed > this.maxSpeed) this.speed = this.maxSpeed;
if (speed < 0) this.speed = 0.0;
else this.speed = speed;

}
Or perhaps you always needs the initial speed to be zero, but needs the maximum speed and license plate to be specified:
Car(String licensePlate, double maxSpeed) {

this.licensePlate = licensePlate;
this.speed = 0.0;
if (maxSpeed > 0) this.maxSpeed = maxSpeed;
else this.maxSpeed = 0.0;

}


Related Discussions:- What is constructors and explain with an example

What is a target , A target is the class that is being advised. The class ...

A target is the class that is being advised. The class can be a third party class or your own class to which you require to add your own custom behavior. By using the concepts of A

Need to develop chat application for j2me, Need to develop Chat application...

Need to develop Chat application for j2me and I want Image Processing code Project Description: We want a chat application for a community website. Image processing and co

File and random access file classes, Difference between the File and Random...

Difference between the File and Random Access File classes? Ans) The File class encapsulates the files and directories of the local file system. The Random Access File class giv

Describe what is meant by a constructor in java, Question: (a) Describe...

Question: (a) Describe what is meant by a ‘constructor' in Java. Use a simple example to illustrate your answer. (b) Differentiate between a class method and an instance me

Implement a driver program to test out all constructors, Implement a class ...

Implement a class  Book  (Book.java). A book will have three instance variables: a  title  (a String, for example: "Big Java" or "The Hitchhiker's Guide to the Galaxy"), an  author

Minimum spanning tree based heuristic, Problem description: A travelling s...

Problem description: A travelling salesman wants to make a tour of the cities and returns back to the starting point. What is the minimum length tour? Formal Definiti

How to passing parameters to applets, How to Passing Parameters to Applets ...

How to Passing Parameters to Applets ? Parameters are passed to applets in NAME=VALUE pairs in tags among the opening and closing APPLET tags. Inside the applet, you read th

Static variable, import java.text.NumberFormat; import java.util.Locale; p...

import java.text.NumberFormat; import java.util.Locale; public class Client {    // instance data    private String name;    private long income_this_year;    private double perce

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd