Results 1 to 8 of 8
  1. #1
    chan_nguyen is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default 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?

    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 );
    	}
    }
    Errors:
    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)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by chan_nguyen View Post
    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?

    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 );
    	}
    }
    You forgot to define the type of reference variable 'ins', so you get compilation errors. Something like this:

    Java Code:
    Map<Integer, Integer> ins= new TreeMap<Integer, Integer>();
    kind regards,

    Jos

  3. #3
    chan_nguyen is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default

    Hi Jos,

    Thanks for your quick response. However, I think I actually define the "ins"
    Java Code:
    ins = new TreeMap<Integer, Integer>();
    in this line?
    Am I misunderstanding something here? If yes, could you help me point it out?

    Thanks,

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by chan_nguyen View Post
    Hi Jos,

    Thanks for your quick response. However, I think I actually define the "ins"
    Java Code:
    ins = new TreeMap<Integer, Integer>();
    in this line?
    Am I misunderstanding something here? If yes, could you help me point it out?

    Thanks,
    That is not a definition; the compiler parses it as an ordinary assignment but it doesn't know the type of variable 'ins'. Read my previous reply for a proper definition of variable 'ins'.

    kind regards,

    Jos

  5. #5
    chan_nguyen is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default

    Hi Jos again,

    Sorry for being careless while copy and edit. The actual variable on that class is :
    Java Code:
    private Object ins;
    instead of
    Java Code:
    private Object instance;
    However, I still got error at compile time:
    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)
    Another question is, is there a way to get rid of cast ( ) while calling a function from Object type, for example :
    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 );
    	}
    }
    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?

    Thanks,

  6. #6
    chan_nguyen is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default

    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?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by chan_nguyen View Post
    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?
    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

  8. #8
    chan_nguyen is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default

    Hi Jos,

    Thanks for your clear explanation. I think I got it ;)

Similar Threads

  1. create new instance of variable class
    By Fedor in forum New To Java
    Replies: 5
    Last Post: 04-12-2009, 08:13 PM
  2. create Instance of class in Javascript
    By TDMaster in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-09-2009, 04:26 PM
  3. Naming a class instance with a variable
    By pikalex88 in forum New To Java
    Replies: 3
    Last Post: 09-30-2008, 06:27 PM
  4. Replies: 1
    Last Post: 05-16-2008, 11:06 PM
  5. A simple generic class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •