Controlling the initial state of objects Assignment Help

Assignment Help: >> Constructors in Java >> Controlling the initial state of objects

Controlling the initial state of objects:

In order to specify the initial state of an object when it is constructed, we can use a constructor. A constructor is a piece of code that contains instructions on how to initialize objects of a class. We can write our own constructors so that we can control how objects are initialized.

A constructor must have the same name as its class. In this section we use a simple example, the class Coordinate. We show this class with an example public access level constructor below:

public class Coordinate
{
// instance variables
private int xPos, yPos;
// a public Coordinate constructor
public Coordinate (int xVal, int yVal)
{
xPos = xVal;

yPos = yVal;

}

// methods of the class

}

In this example the constructor has two arguments, which are used to pass initial values of the two Coordinate instance variables.

A constructor creates an object when you invoke it using the keyword new. For example, the code: new Coordinate(2, 2) creates a new object described by the class Coordinate and sets each of its instance variables to the value 2. The types and order of any arguments in such an expression must match those of the arguments of some constructor for the class, just as for a method invocation. To store a reference to a Coordinate object in a reference newCoord we could write a line like the following example: Coordinate newCoord = new Coordinate(20, -2) Although constructors look like methods, according to the language specification they are not. One notable difference is that the code for the constructor does not declare a returned type. The type it returns in this case is actually Coordinate, but we do not write this in the constructor header. If we were to write a return type, such as void or int, the compiler would interpret the code as being a method.

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your XXXXXX homework and assignments? Live XXXXXX experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer XXXXXX homework help, java assignment help and XXXXXX projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

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