Results 1 to 8 of 8
- 09-07-2010, 05:45 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 13
- Rep Power
- 0
How to check instance of a generic class?
Hi everyone,
I tried to check an instance of Object type with a JFS' class TreeMap<T1, T2>, but I don't know how. Could anyone help me out?
Errors:Java Code:import java.util.TreeMap; public class TreeOrHashMap { private Object instance; public void doSomething() { ins = new TreeMap<Integer, Integer>(); if( ins instanceof TreeMap<Integer, Integer> ) TreeMap<Integer, Integer> m = ( TreeMap<Integer, Integer> )( ins ); m.put( 1, 1 ); } }
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
ins cannot be resolved to a variable
ins cannot be resolved to a variable
Syntax error on token ")", { expected after this token
ins cannot be resolved to a variable
Syntax error, insert "}" to complete Statement
at TreeOrHashMap.doSomething(TreeOrHashMap.java:9)
at Program.main(Program.java:8)
- 09-07-2010, 05:57 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 09-07-2010, 06:01 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 13
- Rep Power
- 0
Hi Jos,
Thanks for your quick response. However, I think I actually define the "ins"
in this line?Java Code:ins = new TreeMap<Integer, Integer>();
Am I misunderstanding something here? If yes, could you help me point it out?
Thanks,
- 09-07-2010, 06:22 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 09-07-2010, 06:24 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 13
- Rep Power
- 0
Hi Jos again,
Sorry for being careless while copy and edit. The actual variable on that class is :
instead ofJava Code:private Object ins;
However, I still got error at compile time:Java Code:private Object instance;
Another question is, is there a way to get rid of cast ( ) while calling a function from Object type, for example :Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot perform instanceof check against parameterized type TreeMap<Integer,Integer>. Use the form TreeMap<?,?> instead since further generic type information will be erased at runtime
Syntax error on token ")", { expected after this token
Syntax error, insert "}" to complete Statement
at TreeOrHashMap.doSomething(TreeOrHashMap.java:10)
at Program.main(Program.java:8)
If I uncomment those three lines, it works. But if I call it through an instance of type Object, it always asks me to cast "ins" to TreeMap. Is there a way work around this problem?Java Code:import java.util.TreeMap; public class TreeOrHashMap { private Object ins; TreeMap<Integer, Integer> makeTreeMap() { ins = new TreeMap<Integer, Integer>(); TreeMap<Integer, Integer> temp = ( TreeMap<Integer, Integer> )( ins ); return temp; } public void doSomething() { ins = makeTreeMap(); //ins = new TreeMap<Integer, Integer>(); //TreeMap<Integer, Integer> temp = ( TreeMap<Integer, Integer> )( ins ); //temp.put( 1, 1 ); ins.put( 1, 1 ); } }
Thanks,
- 09-07-2010, 06:30 PM #6
Member
- Join Date
- Sep 2010
- Posts
- 13
- Rep Power
- 0
Hi Jos,
Thanks for clear explanation. Now I know what is "definition". My problem is that, I want to factor out the common part of TreeMap and HashMap. In other words, I want to use an instance of type object for both data structures, so I don't have duplicate code everywhere. I came from C++ background, so I was influenced strongly by C++ generic. I just try to find a way to do the same thing in Java. Could you help me out?
- 09-07-2010, 07:33 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
C++ templates and Java generics are differnt beasts; Java generics are a compile time only thing while C++ templates generate code everywhere (code bloat). When you cast something the compiler can only check the cast when it is an 'up cast', i.e. if you cast a type D to type B the compiler can handle it if B is a superclass of type D. Otherwise the cast generates code for the JVM that has to handle it. Due to 'type erasure' the JVM can only check the 'raw' type, i.e. there is no way for the JVM to check the difference (there isn't any) between, say, Map<Integer, Integer> and Map<Double, Double>. I consider that a severe limitation of generics; otoh I consider the way those C++ template code generation quite clumsy too; you can't leave that all to those poor linkers ;-)
kind regards,
Jos
- 09-08-2010, 04:20 AM #8
Member
- Join Date
- Sep 2010
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
create new instance of variable class
By Fedor in forum New To JavaReplies: 5Last Post: 04-12-2009, 08:13 PM -
create Instance of class in Javascript
By TDMaster in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 03-09-2009, 04:26 PM -
Naming a class instance with a variable
By pikalex88 in forum New To JavaReplies: 3Last Post: 09-30-2008, 06:27 PM -
The type Class is not generic; it cannot be parameterized with arguments <T>
By new_2_java in forum Advanced JavaReplies: 1Last Post: 05-16-2008, 11:06 PM -
A simple generic class
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks