Answer to Question #72072 in Java | JSP | JSF for Atif Haroon

Question #72072
what is variable- length argument list ?. demonstrate by an example code.
what is copy constructor ?. give an example
1
Expert's answer
2017-12-22T04:42:21-0500
Variable Length Argument Lists
Since Java 5.0, methods can have a variable length argument list. Called varargs, these methods are declared such that the last (and only the last) argument can be repeated zero or more times when the method is called. The vararg parameter can be either a primitive or an object. An ellipsis (…) is used in the argument list of the method signature to declare the method as a vararg. The syntax of the vararg parameter is:

type... objectOrPrimitiveName
The following is an example of a signature for a vararg method:

public setDisplayButtons(int row,
String... names) {...}
The Java compiler modifies vararg methods to look like regular methods. The previous example would be modified at compile time to:

public setDisplayButtons(int row,
String [] names) {...}
It is permissible for a vararg method to have a vararg parameter as its only parameter.

// Zero or more rows
public void setDisplayButtons (String... names) {...}
A vararg method is called the same way an ordinary method is called except that it can take a variable number of parameters, repeating only the last argument.

setDisplayButtons("Jim");
setDisplayButtons("John", "Mary", "Pete");
setDisplayButtons("Sue", "Doug", "Terry", "John");
Source: https://www.safaribooksonline.com/library/view/java-pocket-guide/9780596514198/ch05s02.html

A copy constructor is a constructor that creates a new object using an existing object of the same class and initializes each instance variable of newly created object with corresponding instance variables of the existing object passed as argument. This constructor takes a single argument whose type is that of the class containing the constructor.
Example

public final class Galaxy {

/**
* Regular constructor.
*/
public Galaxy(double aMass, String aName) {
fMass = aMass;
fName = aName;
}

/**
* Copy constructor.
*/
public Galaxy(Galaxy aGalaxy) {
this(aGalaxy.getMass(), aGalaxy.getName());
//no defensive copies are created here, since
//there are no mutable object fields (String is immutable)
}

/**
* Alternative style for a copy constructor, using a static newInstance
* method.
*/
public static Galaxy newInstance(Galaxy aGalaxy) {
return new Galaxy(aGalaxy.getMass(), aGalaxy.getName());
}

public double getMass() {
return fMass;
}

/**
* This is the only method which changes the state of a Galaxy
* object. If this method were removed, then a copy constructor
* would not be provided either, since immutable objects do not
* need a copy constructor.
*/
public void setMass(double aMass){
fMass = aMass;
}

public String getName() {
return fName;
}

// PRIVATE
private double fMass;
private final String fName;

/** Test harness. */
public static void main (String... aArguments){
Galaxy m101 = new Galaxy(15.0, "M101");

Galaxy m101CopyOne = new Galaxy(m101);
m101CopyOne.setMass(25.0);
System.out.println("M101 mass: " + m101.getMass());
System.out.println("M101Copy mass: " + m101CopyOne.getMass());

Galaxy m101CopyTwo = Galaxy.newInstance(m101);
m101CopyTwo.setMass(35.0);
System.out.println("M101 mass: " + m101.getMass());
System.out.println("M101CopyTwo mass: " + m101CopyTwo.getMass());
}
}


Example run of this class:
>java -cp . Galaxy
M101 mass: 15.0
M101Copy mass: 25.0
M101 mass: 15.0
M101CopyTwo mass: 35.0

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS