Results 1 to 3 of 3
  1. #1
    JavaLava is offline Member
    Join Date
    Jun 2013
    Posts
    13
    Rep Power
    0

    Default Class::new Lamba Expression, how does it work?

    When utilizing the Class:new expression to reference a constructor, how does it do so without knowing the parameters to pass in, and how would this work with multiple constructors (overloaded)?

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Class::new Lamba Expression, how does it work?

    Interesting question. At first I though that is not possible. But after some research there is a better answer than "It's not possible".

    If you have a functional interface, the object can be created using a parameter. Look at this example:
    Java Code:
    class Scratch {
        public static void main(String[] args) {
            Blah blah1 = String::new;
        }
    
        interface Blah {
            public String create(char[] chars);
        }
    }
    This looks weird, and for simplicity's and maintenance sake, I would not recommend this. But here's how it works:

    Blah is a functional interface: it only has one method, which returns a String. The name of the method is really not that important at this point, since there can be only ONE method in a functional interface. The compiler will look for a constructor that matches the signature of the return type with the parameter. In this case, it will look for a String constructor that takes a char-array.

    This example might make it a bit clearer:
    Java Code:
    class Scratch {
        public static void main(String[] args) {
            WhateverFunction blah3 = Whatever::new;
            WhateverFunction blah4 = i -> new Whatever(i);
    
            Whatever w3 = blah3.create(1); 
            Whatever w4 = blah4.create(1); 
        }
    
        interface WhateverFunction {
            public Whatever create(int i);
        }
    
        static class Whatever {
            public Whatever(int i) {
            }
        }
    }
    Note that blah3 and blah4 are the same.

    The WhateverFunction returns a Whatever object in its only functional method. The required int is passed onto the constructor of the result type, being Whatever(int i).
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    JavaLava is offline Member
    Join Date
    Jun 2013
    Posts
    13
    Rep Power
    0

    Default Re: Class::new Lamba Expression, how does it work?

    Thanks for the reply, I am getting an error while passing in Spear_Entity::new as a parameter to a method. The method takes an interface called IFactory, as the first parameter. I have a class as follows :

    Java Code:
    public class Spear_Entity extends TridentEntity implements IFactory<Spear_Entity> {
    	
    	public Spear_Entity(EntityType<? extends TridentEntity> p_i50148_1_, World p_i50148_2_) {
    		
    		super(p_i50148_1_, p_i50148_2_);
    	}
    
    	public Spear_Entity(World worldIn, PlayerEntity playerentity, ItemStack stack) {
    
    		super(worldIn, playerentity, stack);
    	}
    
    	@Override
    	public Spear_Entity create(EntityType<Spear_Entity> p_create_1_, World p_create_2_) {
    
    		return null;
    	}
    }
    The error is "Cannot infer type argument(s) for <T> create(EntityType.IFactory<T>, EntityClassification)", it only happens when the second constructor exists, but not if I comment it out. Help needed, thanks.

Similar Threads

  1. Replies: 3
    Last Post: 02-04-2013, 01:44 PM
  2. How do .class files work?
    By Oddblob in forum New To Java
    Replies: 2
    Last Post: 01-07-2013, 06:11 PM
  3. Replies: 2
    Last Post: 07-18-2011, 08:11 AM
  4. regular expression matching does not work
    By flok in forum Advanced Java
    Replies: 2
    Last Post: 12-09-2009, 04:38 PM
  5. Replies: 9
    Last Post: 08-08-2007, 08:16 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
  •