Results 1 to 3 of 3
Thread: Writing generic methods
- 12-30-2007, 12:30 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 49
- Rep Power
- 0
Writing generic methods
Hi,
I am writing an application in which I have following 4 methods:
I want to declare a method that should be generic and that should serve all the purposes.Java Code:int startProcess(int a) { ... } String startProcess(String a) { ... } float startProcess(float a) { ... }
Please advice.
- PEACE
- 12-30-2007, 04:27 PM #2
Generics
Hello eva.
How about using objects of instance Object. Like:
You can then use casting to get the correct data type depending on what you want to do. To implement this method, you will need to use the instanceof keyword. For example:Java Code:public static Object startProcess(Object a) { //Todo return null; }
I hope this helped.Java Code:public static Object startProcess(Object a) { // Handel if it is a String if (a instanceof String){ String aString = (String) a; // Do something to aString } return aString; }Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 12-31-2007, 03:28 AM #3
Member
- Join Date
- Dec 2007
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
A generic framework for a flexible, multi-threaded server
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:14 PM -
Generic array
By eva in forum New To JavaReplies: 3Last Post: 12-23-2007, 12:12 AM -
Generic Hashtables
By ShoeNinja in forum New To JavaReplies: 0Last Post: 12-04-2007, 10:43 PM -
Generic Eclipse Modeling System 3.0 RC1
By JavaBean in forum Java SoftwareReplies: 0Last Post: 10-25-2007, 04:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks