Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default perfect method parameter/argument match rejected. why?

    I've made the code as generic as possible to avoid distraction.
    I'm using jdk 1.6u22 on a win7 pro 64bit.
    I'm getting a compiler error that contradicts itself to my understanding. Can anybody understand how to fix this?

    Compiler Error:
    remove(Class1<T>.nestClass<T>) in Class1<T> cannot be applied to (Class1<T>.nestClass<T>)

    public class Class1<T>{

    private void remove(nestClass<T> current){
    }

    private class nestClass<T>{
    }

    public class ClassIterator<T>{ //will extend Iterator<T> once this problem is fixed
    private Class1<T> c;
    private nestClass<T> mySubClass;

    public void remove(){
    c.remove(mySubClass);
    }
    }
    }

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    I played around with it to get it to compile. I'm not that great with generics yet, I think it was a problem with having all the classes in one file or having multiple classes under Class1.

    Java Code:
    public class Class1<T>
    {
    	void remove(nestClass<T> mySubClass)
    	{
    	}
    }
    
    class nestClass<T>
    {
    }
    
    class ClassIterator<T>
    {
    	private Class1<T> c;
    	private nestClass<T> mySubClass;
    
    	public void remove()
    	{
    		c.remove(mySubClass);
    	}
    }
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Why is the inner ClassIterator paramatised?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    My previous post might have been a bit terse...

    So to directly address your question about the compiler message "foo(<some arg type>) cannot be applied to <some arg type>": there is only one .class file for the inner class that does for all the different types T. An inner class "knows about" the type argument of its outer class and, at runtime, an object of the inner class type will have a hidden reference to an object of the outer class type.

    Taking advantage of this, a straightforward implementation of Iterator<T> might look like:

    Java Code:
    public class Class1<T> {
    
        private void remove(nestClass<T> current) {}
    
        private class nestClass<T> {}
    
        public class ClassIterator implements Iterator<T> {
            private Class1<T> c;
            private nestClass<T> mySubClass; // <-- this thing is a Class1<T>.nestClass<T>
    
            public void remove() {
                c.remove(mySubClass);
            }
    
            public boolean hasNext() {
                // ...
            }
    
            public T next() {
                // ...
            }
        }
    }

    (What next() would actually return is part of the distracting detail.)

    -----

    Now if you really intend ClassIterator to be parametised it would be clearer (to a human reader; the compiler is in no doubt) to say:

    Java Code:
    public class Class1<T> {
    
        private void remove(nestClass<T> current) {}
    
        private class nestClass<T> {}
    
        public class ClassIterator<T2> {
            private Class1<T2> c;
            private nestClass<T2> mySubClass; // <-- this thing is a Class1<T>.nestClass<T2>
    
            public void remove() {
                c.remove(mySubClass);  // ***
            }
        }
    }

    It still won't compile, but at least the compiler's message is less cryptic: "required Class1<T2>.nestClass<T2> but found Class1<T>.nestClass<T2>".

    -----

    Check out AbstractList.java to see how the inner classes implement parametised interfaces but are not themselves parametised:

    Java Code:
    public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
    
        public Iterator<E> iterator() {
            return new Itr();
        }
    
        private class Itr implements Iterator<E> {
            // methods that manipulate a cursor...
        }
    }
    Last edited by pbrockway2; 10-30-2010 at 12:50 AM.

  5. #5
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default Problem solved

    Thanks Josh and pbrockway2. I ended up going with pbrockway2's method to solve the compile error because it is in line with sun's java documentation, and works the way I want it to. Have a good one guys.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

Similar Threads

  1. Method as an argument?
    By StokedOnMe in forum New To Java
    Replies: 13
    Last Post: 09-18-2009, 07:29 AM
  2. Replies: 4
    Last Post: 03-03-2009, 10:39 AM
  3. Java Developer - Need Job - This is Perfect match
    By ravisrivastava2003 in forum Java Software
    Replies: 0
    Last Post: 12-10-2008, 08:35 PM
  4. Can a method take itself as parameter?
    By bukake in forum New To Java
    Replies: 10
    Last Post: 09-06-2008, 10:26 PM
  5. Input parameter of Main method
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 07-12-2008, 07:24 PM

Tags for this Thread

Posting Permissions

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