Returning an object from a method in Java
Returning an object from a method Generating Code 3 Of 9 In Java Using Barcode creator for Java Control to generate, create Code39 image in Java applications. Believe it or not, the previous sections on parameter passing left one nook and cranny of Java methods unexplored When you call a method, the method can return something right back to the calling code In previous chapters and sections, I return primitive values, such as int values, or nothing (otherwise known as void) In this section, I return a whole object It s an object of type City from Listing 10-12 The code that makes this happen is in Listing 10-14 Bar Code Maker In Java Using Barcode drawer for Java Control to generate, create bar code image in Java applications. 10: Putting Variables and Methods Where They Belong
Barcode Reader In Java Using Barcode decoder for Java Control to read, scan read, scan image in Java applications. Listing 10-14: Here, Have a City
Code-39 Drawer In C# Using Barcode creation for .NET framework Control to generate, create Code 39 Extended image in Visual Studio .NET applications. class TrackPopulation4 { public static void main(String args[]) { City smackoverAR = new City(); smackoverARpopulation = 2232; smackoverAR = doBirth(smackoverAR); Systemoutprintln(smackoverARpopulation); } static City doBirth(City aCity) { City myCity = new City(); myCitypopulation = aCitypopulation + 1; return myCity; } } If you run the code in Listing 10-14, you get the number 2,233 That s good The code works by telling the doBirth method to create another City instance In the new instance, the value of population is 2333 (Figure 10-13) Make ANSI/AIM Code 39 In VS .NET Using Barcode maker for VS .NET Control to generate, create Code39 image in .NET applications. main smackoverAR
Code 39 Extended Drawer In Visual Basic .NET Using Barcode generation for VS .NET Control to generate, create Code 39 image in Visual Studio .NET applications. population 2232
Data Matrix 2d Barcode Generation In Java Using Barcode maker for Java Control to generate, create ECC200 image in Java applications. Figure 10-13: The doBirth method creates a City instance
Encode Code 39 Full ASCII In Java Using Barcode printer for Java Control to generate, create Code 39 image in Java applications. doBirth aCity population myCity 2233
Barcode Generator In Java Using Barcode maker for Java Control to generate, create barcode image in Java applications. After the doBirth method is executed, that City instance is returned to the main method Then, back in the main method, that instance (the one that doBirth returns) is assigned to the smackoverAR variable (See Figure 10-14) Now smackoverAR refers to a brand-new City instance an instance whose population is 2,233 EAN13 Generation In Java Using Barcode drawer for Java Control to generate, create European Article Number 13 image in Java applications. Part IV: Savvy Java Techniques
EAN / UCC - 13 Maker In Java Using Barcode drawer for Java Control to generate, create GS1 128 image in Java applications. main smackoverAR
Generate British Royal Mail 4-State Customer Code In Java Using Barcode generation for Java Control to generate, create British Royal Mail 4-State Customer Code image in Java applications. Figure 10-14: The new City instance is assigned to the smackoverAR variable
Drawing Code 128C In Visual Basic .NET Using Barcode generator for .NET Control to generate, create Code 128 Code Set A image in .NET applications. doBirth
Recognize Barcode In .NET Using Barcode decoder for .NET Control to read, scan read, scan image in .NET applications. population myCity 2233
Making Bar Code In Visual C# Using Barcode drawer for Visual Studio .NET Control to generate, create bar code image in Visual Studio .NET applications. In Listing 10-14, notice the type consistency in the calling and returning of the doBirth method: The smackoverAR variable has type City The smackoverAR variable is passed to the aCity parameter, which is also of type City The myCity variable is of type City The myCity variable is sent back in the doBirth method s return statement That s consistent, because the doBirth method s header begins with the promise public static City the promise to return an object of type City The doBirth method returns an object of type City Back in the main method, the object that the call to doBirth returns is assigned to the smackoverAR variable, and (you guessed it) the smackoverAR variable is of type City Aside from being very harmonious, all this type agreement is absolutely necessary If you write a program in which your types don t agree with one another, the compiler spits out an unsympathetic incompatible types message Draw Data Matrix ECC200 In .NET Using Barcode drawer for .NET framework Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications. Epilogue
Painting Barcode In .NET Framework Using Barcode creator for ASP.NET Control to generate, create bar code image in ASP.NET applications. Dora Kermongoos and her newborn baby daughter are safe, healthy, and resting happily in their Smackover, Arkansas, home UPC - 13 Printer In C# Using Barcode generator for Visual Studio .NET Control to generate, create EAN-13 Supplement 5 image in .NET framework applications. 11
Barcode Generator In C#.NET Using Barcode generator for .NET Control to generate, create barcode image in .NET framework applications. Using Arrays and Collections to Juggle Values
Recognizing Code-39 In .NET Using Barcode reader for .NET Control to read, scan read, scan image in VS .NET applications. In This
Dealing with several values at once Creating values as you get a program running Impressing other programmers with fancy generic types elcome to the Java Motel! No haughty bellhops, no overpriced room service, none of the usual silly puns Just a clean double room that s a darn good value! Getting Your Ducks All in a Row
The Java Motel, with its ten comfortable rooms, sits in a quiet place off the main highway Aside from a small, separate office, the motel is just one long row of ground floor rooms Each room is easily accessible from the spacious front parking lot Oddly enough, the motel s rooms are numbered 0 through 9 I could say that the numbering is a fluke something to do with the builder s original design plan But the truth is that starting with 0 makes the examples in this chapter easier to write Anyway, you re trying to keep track of the number of guests in each room Because you have ten rooms, you may think about declaring ten variables: int guestsInRoomNum0, guestsInRoomNum1, guestsInRoomNum2, guestsInRoomNum3, guestsInRoomNum4, guestsInRoomNum5, guestsInRoomNum6, guestsInRoomNum7, guestsInRoomNum8, guestsInRoomNum9; Part IV: Savvy Java Techniques
Doing it this way may seem a bit inefficient But inefficiency isn t the only thing wrong with this code Even more problematic is the fact that you can t loop through these variables To read a value for each variable, you have to copy the nextInt method ten times guestsInRoomNum0 = diskScannernextInt(); guestsInRoomNum1 = diskScannernextInt(); guestsInRoomNum2 = diskScannernextInt(); and so on Surely a better way exists That better way involves an array An array is a row of values, like the row of rooms in a one-floor motel To picture the array, just picture the Java Motel: First, picture the rooms, lined up next to one another Next, picture the same rooms with their front walls missing Inside each room you can see a certain number of guests If you can, forget that the two guests in Room 9 are putting piles of bills into a big briefcase Ignore the fact that the guests in Room 6 haven t moved away from the TV set in a day and a half Instead of all these details, just see numbers In each room, see a number representing the count of guests in that room (If freeform visualization isn t your strong point, look at Figure 11-1)
|
|
|
|
| ALL RIGHTS RESERVED. Business Refinery (c) 2006 - 2010. | Terms of Use | Privacy Policy |