Helpful Information
 
 
Category: Java
returning multiple items

if i needed to make a method to get a full name and return first and last names would i do it somehting like this?



public String getFullName
{
return firstName;
return lastName;
or
return firstName lastName
}


assuming they were defined else where how would i return both items?

A method usually terminates when the statment is returned. You will need to encapsulate the two objects. Java only allows you to return one type.


public int returnNP(int V){
n = -F/E;
int P = (n*R*T)/V;
// ideal gas Law

Return n, P; // will not work
}


int[] nAndP = new int[];
public int [] returnNR(int V){
n = -F/E;
int P = (n*R*T)/V;
// ideal gas Law
nAndP[0] = n;
nAndP[1] = P;
Return nAndP; // will work
}










privacy (GDPR)