Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-03-2010
Member
 
Join Date: Mar 2010
Posts: 200
Implement the return type in java

Hello,
I am about to write a class method and return an object of type that has passed the class argument. I know a method of the object EntityManager the fact:
Here is the code which I will be using
Code:
<T> Find T(<T> EntityClass Class, Object primaryKey)
But how to implement it? I can not find a code that does it. Any help will be appreciated. Thanks in advance.
Reply With Quote
  #2  
Old 25-03-2010
Member
 
Join Date: Nov 2009
Posts: 583
Re: Implement the return type in java

Hello,
You might be a bit more explicit? Why do you return a type that you pass an argument? If it is, as I suppose, because the type passed as argument change (normal), why not create an interface or class? This will be easy to do and it will be helpful in future if you want to import the class and it's methods. I recommend you to do some study on the object oriented programming.
Reply With Quote
  #3  
Old 25-03-2010
Member
 
Join Date: Dec 2009
Posts: 213
Re: Implement the return type in java

Hello,
I am interested in this topic and I have done some study on this but the method is to search for any object type. I must just give the class and object and it will send me the object correctly cast. It works if my return is Object, but it forced me to return the caster.
But finally I found. I tried everything, except the solution
Code:
Public <T> Find T(<T> Class class, Object pk)
Instead of T, we can put what you want except? It helps to have a return type configurable.
Reply With Quote
  #4  
Old 25-03-2010
Member
 
Join Date: Nov 2009
Posts: 356
Re: Implement the return type in java

Hello,
This method is part of a class "service" that links with the EntityManager. You can try something like this.
Here is the code
Code:
	Public <T> Find T(<T> Class class, Object primaryKey) {
		return em.find(class, pk);
	}
With this method, you might as well get a "moving target" that subject "bill", no cast.
Code:
Invoice inv = service.find(bill.class, pkFacture);
Movement mt = service.find(Stroke.class, pkMvmt);
Reply With Quote
  #5  
Old 25-03-2010
Member
 
Join Date: Nov 2009
Posts: 335
Re: Implement the return type in java

Hello,
Yes of course! It is simply the use of Generics on a method and not to the scale of the class (the <T> is before the method definition). It is very convenient because it saves them the relationship between the parameters and the method returns. It is widely used in the classroom Collections among others. Hope this will help you and you will get going with your program.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Implement the return type in java"
Thread Thread Starter Forum Replies Last Post
Implement Simple Database using BST in Java maxmidg412 Software Development 2 31-07-2011 03:22 AM
Implement Java tic tac toe SmokiN Software Development 3 29-06-2009 09:09 AM
Static types can we used as return type ComPaCt Software Development 2 12-06-2009 03:37 PM
ArrayList and type the return manjava Software Development 2 09-05-2009 11:15 PM
How to implement MultiDimensional Array in JAVA Nihar Khan Software Development 3 28-02-2009 01:25 PM


All times are GMT +5.5. The time now is 11:59 PM.