Returning An instance of an Object

how to return an instance of a class java
how to return an object in java
java object return itself
java return this
return object type java
how to return array of objects in java
java pass object by reference
java get instance of class

In my code I was instructed to return an object in my GetCustomer method in my CustomerManager class from the Customer class . I am new to object oriented programming so I don't have any ideas on how to do this. Can someone help me out?

Customer Manager Class

public class customerManager
{
    private static int currentCusNo;
    private int maxCustomers;
    private int numCustomers;
    customer[] cList;

    public customerManager(int maxCust, int seed)
    {
        currentCusNo = seed;
        maxCustomers = maxCust;
        numCustomers = 0;
        cList = new customer[maxCustomers];
    }
    public bool addcustomers(string fN, string lN, string ph)
    {
        if (numCustomers > maxCustomers) return false;

        customer m = new customer(fN, lN, ph, currentCusNo);
        currentCusNo++;
        cList[numCustomers] = m;
        numCustomers++;
        return true;

    }
    public int findCustomer(int cusID)
    {
        for (int x = 0; x < numCustomers; x++)
        {
            if (cList[x].getID() == cusID)
            {
                return x;
            }
        }
        return -1;
    }
    public bool customerExist(int cusID)
    {
        for (int x = 0; x < numCustomers; x++)
        {
            if (cList[x].getID() == cusID)
            {
                return true;
            }
        }
        return false;
    }
    public string customerlist()
    {
        string y = " ";

        for (int x = 0; x < numCustomers; x++)
        {

            y += "\nFirst Name: " + cList[x].getFirstName() + "\nLast name: " + cList[x].getLasttName() + "\nCustomer ID: " + cList[x].getID();

        }

        return y;
    }
    public customer GetCustomer(int cID)
    {
        for (int x = 0; x < numCustomers; x++)
        {
        }
    }
}

Customer Class

public class customer
{
    private int customerID;
    private string firstName;
    private string lastName;
    private string phone;

    public customer(string fN, string lN, string ph, int cId)
    {
        customerID = cId;
        firstName = fN;
        lastName = lN;
        phone = ph;
    }
    public int getID() { return customerID; }
    public string getFirstName() { return firstName; }
    public string getLasttName() { return lastName; }
    public string getPhone() { return phone; }

    public string toString()
    {
        string s = "";

        s +="First Name: " + firstName + "\nLast Name: " + lastName + "\nPhone: " + phone + "\nCustomer ID: " + customerID;

        return s;
    }
}

Passing and Returning Objects in Java, But when we pass an object to a method, the situation changes dramatically, because objects are passed by what is effectively call-by-reference. Java does this  When someone says "return an object" in C# they typically mean: "return a reference to the thing I want". Luckily, classes are always stored (at least, for purposes for this discussion) as references already, so this is really simple.

Using your code...

public customer GetCustomer(int cID)
{
    int index = findCustomer(cID);

    if (index != 1)
        return cList[index];

    return null;
}

Find the index of the cID you want and return the item of the list in this index or null if you do not find.

Or ...

Copy and paste the findCustomer code, changing return x; by return cList[x]; and -1 to null;

When should a method of a class return the same instance after , Is it even okay to modify the own instance and return it, too? Or should it only return a copy and leave the original object as before? Because when returning the  In C++ programming, object can be returned from a function in a similar way as structures. Example 2: Pass and Return Object from the Function In this program, the sum of complex numbers (object) is returned to the main() function and displayed.

It looks like it would be very similar to your findCustomer method, only that instead of returning an int, you would return the actual cutomer object. If I followed the same pattern of the rest of your class, it would look something like:

    public customer GetCustomer(int cID)
    {
       for (int x = 0; x < numCustomers; x++)
       {
          if (cList[x].getID() == cID)
          {
             return cList[x];
          }
       }
       return null;
    }

It should be said, however, that if you are using System.Linq, then the same method could be done with just one line:

    public customer GetCustomer(int cID)
    {
       return cList.Where(x => x.getID() == cID).SingleOrDefault();
    }

Cheers JM

Retrieving Class Objects (The Java™ Tutorials > The Reflection API , Since arrays are Objects , it is also possible to invoke getClass() on an instance of an array. The returned Class corresponds to an array with component type  This developer guide tells how to Return Object from a Method in JAVA using Java Object Methods. First, we will discuss the return type of any method. Then we will see the control flow from invoking code to invoked code and again when control goes back to invoking code. After learning about method overloading and constructor overloading in Java

C++ Tutorial: Object Returning - 2020, For instance, in statement #2, the program computes the sum of c1 and c2, copies the answer into the temporary return object, overwrites the contents with the  Returning Objects. In java, a method can return any type of data, including objects. For example, in the following program, the incrByTen ( ) method returns an object in which the value of a (an integer variable) is ten greater than it is in the invoking object. filter_none.

Defining an object reference return type, When an object is passed locally, class instances are always returned by reference. Thus, only a reference to an object is returned, not the object itself. When an  If this is what you're doing, you're out of luck - object_to_return has gone out of scope and been destructed at the end of return_Object, so myObject refers to a non-existent object. You either need to return by value, or return an Object declared in a wider scope or new ed onto the heap. share.

Java's Object Methods: getClass(), Below are the methods of the base Java Object present in all Java objects due to actually used the getClass() method to return an instance of the Class class,  I'm not sure how to return an instance of an object from a method with values. Is there some way to convert an int type into my object type? Here is some instructions I have: /* Design a class named Location for locating a maximal value and its location in a two-dimensional array.

Comments
  • I don't see any diference regarding the method you want to implement and the findCustomer you already have, since both receive an id. Can you give us more info on what the method should do?
  • Copy method findMethod rename it to getCustomer in loop return cList[x] ad instead of return false use return null.
  • The code works just as intended now. Thank so much for the helpful tips and explaining how the code works. I will keep them in mind next time!
  • You do know your code with LINQ doesn't compile? Make sure you review what Where does for the future...
  • Instead of Where and First(), you're way better off using Single, or SingleOrDefault in this situation if you don't want the code to break at that moment. No customer should have the same ID, so it'd be better to make the app crash if in the db, there were two customers pointing to the same ID
  • Yep. SingleOrDefault makes the most sense. Either give me an exception if there is more than one for some bizaar reason, or give me null.