Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Variable Type-Checking

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Variable Type-Checking

    Is it possible to write a method whose parameter type is dependent on another parameter? In other words, if I had:

    void set(F fieldtype, V value);

    and the type of value will be checked at compile-time against the value of fieldtype (however fieldtype is implemented)


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Variable Type-Checking

    Not at compile time. You can use reflection to type-check at run time. However, I would strongly recommend refactoring your code if you're resorting to doing that.

    edit: haha, got confused on the terminology. It's reflection, not refraction.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Variable Type-Checking

    I got the "reflection" part, .

    Truth is, I'm actually refactoring code right now because the way we've implemented it (using J2SE) is we use method overloading to implement it. Basically, we use:

    function1(FieldType1, ValueType1);
    function2(FieldType2, ValueType2);
    ...
    functionN(FieldTypeN, ValueTypeN);

    which implements the same functionality but are able to use Java's method signature resolution at compile-time. I was hoping it could be made generic.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Variable Type-Checking

    Hmm, if you have the same functionality regardless of the type, you can either use generic parameterization or polymorphism. Without knowing more about what it is you want to do I can't determine which one to use (both should work in theory). I tend to lean towards polymorphism because it's more clean and concise, but could require more code refactoring than is necessary (especially if the original code was never designed to take advantage of inheritance/polymorphism).

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Variable Type-Checking

    May or may not suit this scenario, but you could extend one of the types from the other
    public class MyClass<V, T extends V>{
    ///my class
        public void function(V v, T t){
     
        }
    }

    If necessary you could wrap any values into another object/interface to force compile time checking, but again I'm not fully sure this will suit your scenario (as well as being simply overkill)

  6. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Variable Type-Checking

    I tried the following snippet and it's not working as I was hoping it would, I'm coding out of ignorance right now:

    package blah;
     
    import java.util.Hashtable;
    import java.util.Map;
     
    public class TestModel {
    	private Map<Object, Object> container = new Hashtable<Object, Object>();
     
    	public static void main(String[] args) {
    		TestModel v = new TestModel();
    		v.add("hello", new Integer(2)); // There should be a compile-time error here
    		v.add("hello", "there"); // But not here
    		System.out.println("Value = " + v.getContainer().get("hello"));
    	}
     
    	public<K> void add(K fieldType, K value) {
    		container.put(fieldType, value);
    	}
    	public void setContainer(Map<Object, Object> container) {
    		this.container = container;
    	}
     
    	public Map<Object, Object> getContainer() {
    		return container;
    	}
    }

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Variable Type-Checking

    a) Define your class to be Generic
    public class TestModel<K>{

    b) no need for the generic near the public
    public void add(K fieldType, K value)//remove the 'public<K>'

  8. #8
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Variable Type-Checking

    Quote Originally Posted by copeg View Post
    a) Define your class to be Generic
    public class TestModel<K>{

    b) no need for the generic near the public
    public void add(K fieldType, K value)//remove the 'public<K>'
    But this is tying down the class type for value at the time the model variable is defined. I was hoping to tie it down to the fieldType parameter at compile-time. Note the two usage examples I had and the expected results:
    v.add("hello", new Integer(2)); // There should be a compile-time error here
    v.add("hello", "there"); // But not here

Similar Threads

  1. checking for empty strings?
    By Celeadyne in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2010, 04:18 PM
  2. Is there an integer-type variable bigger than "long"?
    By bardd in forum Java Theory & Questions
    Replies: 2
    Last Post: September 3rd, 2010, 02:43 PM
  3. Not sure what type of variable to use.
    By mjpam in forum Java Theory & Questions
    Replies: 13
    Last Post: July 13th, 2010, 08:58 PM
  4. Checking in.
    By Johannes in forum Member Introductions
    Replies: 7
    Last Post: August 13th, 2009, 06:11 AM
  5. [SOLVED] Getting Exception " java.util.InputMismatchException" in scanner package
    By luke in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 20th, 2009, 04:55 AM