This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Help me out to find a solution to the below program.
1) I have declared an array called student  in main program 2)I initialised the array inside the function void modifyRef().I can print the values inside the function.I need to get the values returned in the main function. 3)But the constraint is I should not use return type in the function. 4)I need only the values and not the objects.
If you must create the array object inside the method, and you must return void, then one choice is for the method to store a reference to the array someplace where the caller can get it, like in a member variable of the class. Another idea is to use the Java equivalent of "pass by reference:" make the argument to the method be an array of arrays of Student, and pass in a one-element-long, empty Student as an argument. The method can then store the array in the single element of this array, and the caller will be able to retrieve it from there.
These are both really terrible designs, though; can I ask why you have these restrictions in the first place?
Just so people don't get confused, I posted an answer saying that the values were already in main(), before I noticed that the array was being initialised in modifyRef(). I deleted that, but I guess Vid had already read it.
Anyway, Vid, the answer to your new question is as posted in my first reply. 1. Create the array in main() 2. Populate it in modifyRef() 3. Print it out in main().
Joined: Aug 17, 2006
The requirement is I need to return only the values of API and not the objects.