• Post Reply Bookmark Topic Watch Topic
  • New Topic

TypeFactory can it be done in java?  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all

i have simple hashmap i need to fill it with String as key and any type can be as value

im getting the input of the key and value as Object type and i sending the results to some kind of type factory class ( that does not work )

and then i fill the hashmap with it but the result im geting is wrong because the types im getting from the type factory class is always as objects .

and i dont want it to be as Objects :

here is my main class :

 
Ashok C. Mohan
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your TypeFactory.getType(Object obj) method returns an "Object". So no matter what casting you do, you will always receive an object. I am not able to understand your requirement. It does not matter if you put a String or any other object into your hashmap. You can always cast it to the appropriate class when you retreive it. Hope it helped. If you can elaborate your requirement, I may be able to help you more!
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what im trying to do is simple build typeFactory
that will get Something from and return its right type
 
Ashok C. Mohan
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still dont understand the need for this. But anyways, I guess you can use Generic methods for this. That way you can return any object from a method.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your TypeFactory class does (almost) nothing. It just returns what you put into it.

The way you are trying to do it, with a TypeFactory class, will not work. Can you please explain why you want to use this TypeFactory class? What do you think you would gain with this idea?

Note that type checking is a compile-time thing; are you trying to do some kind of type checking during runtime?
 
Chris Beckey
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Casting does not change the type of an object. Casting changes a reference to an object to another (compatible) type. Breaking your code down into little chunks:

Th getType method is passed a reference of type Object. The object referenced may be of any type. The instanceof calls determines whether the object is a compatible type to the class named. In the case of String, it must be of type String because that class is final, however if that were not the case then it could be any class derived from String. If String were an interface then it could be any class that implements the String interface, either directly or indirectly.
Once it is determined that the object is an instance of String, the next line (the return) can be thought of as follows:
1.) create a (temporary, internal) reference of type String that points to the same object referenced by the variable "obj". This is the cast operation.
2.) create another (temporary, internal) reference of type Object that points to the same object referenced by the String reference just created.
3.) return the second (temporary, internal) reference.

You may be doing more work than you need to. The referenced object is whatever class it was when it was created, look there to see if it was typed correctly.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all and thanks for the fast reply
i will try to explain what i meant (sorry English is not my native Lang ..)
ok first of all maybe my implementation is wrong ( or defiantly wrong )
i just like to able to build function that will return me different type based on some parameter
and it will give me the returned type on runtime .
can it be done in java ?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you cannot write a method that returns different types based on some parameter the way you want to do it. Note that this idea is also not very useful - you have to assign the result of the method to something (a variable for example), now if you don't know the type of the return value before runtime, then how are you going to determine the type of the variable you want to assign the value to at the moment you're writing the program?

If you make that variable an Object, so that anything fits into it, then the method that returns the unknown type is useless, because you're going to ignore the type of the return value anyway.

Note that there is a distinction between the static type of a variable and the runtime type of the thing that the variable is pointing to. You can do this:

The static type of the variable "obj" is Object, but the dynamic type of the object it refers to is Integer.

If you need a Map that accepts only specific types for the keys and values, then you should use generics:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!