Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-30-2007, 02:30 PM
eva eva is offline
Member
 
Join Date: Dec 2007
Posts: 49
eva is on a distinguished road
Writing generic methods
Hi,

I am writing an application in which I have following 4 methods:

Code:
int startProcess(int a) { ... } String startProcess(String a) { ... } float startProcess(float a) { ... }
I want to declare a method that should be generic and that should serve all the purposes.

Please advice.

- PEACE
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-30-2007, 06:27 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Generics
Hello eva.

How about using objects of instance Object. Like:
Code:
public static Object startProcess(Object a) { //Todo return null; }
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:
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; }
I hope this helped.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-31-2007, 05:28 AM
Member
 
Join Date: Dec 2007
Posts: 12
spoon! is on a distinguished road
it would help more if you told us what this method should do
you could use generics like this:
Code:
public static <E> E startProcess(E a) { ... }
but you won't be able to do very much with the argument unless you know what type it is
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing MIDlet for SMS - V JavaForums Java Blogs 0 04-11-2008 05:20 PM
A generic framework for a flexible, multi-threaded server Java Tip java.net 0 04-07-2008 10:14 PM
Generic array eva New To Java 3 12-23-2007 02:12 AM
Generic Hashtables ShoeNinja New To Java 0 12-05-2007 12:43 AM
Generic Eclipse Modeling System 3.0 RC1 JavaBean Java Announcements 0 10-25-2007 06:25 PM


All times are GMT +3. The time now is 01:16 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org