how to construct objects with new operatorclass


How to Construct objects with new operator?

class Car {

String licensePlate; // e.g. "New York 543 A23"
double speed; // in kilometers per hour
double maxSpeed; // in kilometers per hour

}
To instantiate an object in Java, use the keyword new followed through a call to the class's constructor. Here's how you'd form a new Car variable called c:

Car c;
c = new Car();

The first word, Car, declares the type of the variable c. Classes are types and variables of a class type required to be declared just like variables in which are ints or doubles.

The equals sign is the assignment operator and new is the construction operator.

At last notice the Car() method. The parentheses tell you this is a techniques and not a data type such as the Car on the left hand side of the assignment. This is a constructor, a method in which creates a new instance of a class. You'll learn more about constructors shortly. Therefore if you do nothing, then the compiler inserts a default constructor in which takes no arguments.

This is frequent condensed into one line like this:
Car c = new Car();

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: how to construct objects with new operatorclass
Reference No:- TGS0284490

Expected delivery within 24 Hours