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 actionform, An ActionForm is a JavaBean that extends org.apache.str...

An ActionForm is a JavaBean that extends org.apache.struts.action.ActionForm.  ActionForm handles the session state for web application and the ActionForm object is automatically p

What is jsf life cycle and its phases, The series of steps followed by an a...

The series of steps followed by an application is known its life cycle. A JSF application typically follows six steps in its life. 1. Restore view phase 2. Apply request valu

Integration of neo4j and opengeo, Integration: Neo4J, OpenGeo, Ikanow Pr...

Integration: Neo4J, OpenGeo, Ikanow Project Description: I have an ongoing project to loosely integrate a variety of existing Open Source products: OpenGeo (geospatial server

How much cpu time does an applet get, How much CPU time does an applet get?...

How much CPU time does an applet get? One of the few legitimate concerns about hostile applets is excessive use of CPU time. It is possible on a non-preemptively multitasking s

Application for android studio, 1- I need application used android studio w...

1- I need application used android studio with source code and all file . same this application : 2-">https://play.google.com/store/apps/details?id=com.magnetic.openmaps&hl=en 2

Illustrate object-to-relational mapping?, O/R mapping is well suited for re...

O/R mapping is well suited for read, change, write centric applications and not suited for write centric applications where data is seldom read. Although this was usually true of m

Develope a simple polling web application, Develop a simple polling (voting...

Develop a simple polling (voting) web application according to the following specifications: Initially a page should be presented to the user where he can enter his/her name

what is meant by binding in rmi, Binding is a method of associating or re...

Binding is a method of associating or registering a name for a remote object that can be used at a later time to look up that remote object. A remote object can be linked with a na

Development build to production server tomcat, Move development build to pr...

Move development build to production server tomcat Project Description: Move our development build and integrate with MYSQL database, to our production vps. The software i

What is file transfer protocol, What is File Transfer Protocol? This pr...

What is File Transfer Protocol? This protocol is used to upload the files on remote computers. This is used to transfer files among computer on TCP/IP network e.g. internet and

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