0

Help me with problem at the comment, please. I'm newbie with java8. It would be great too if you give me some keyword for searching.

public class aClass{
    public Function<String,String> f;
    private void doSomething(){
        System.out.println("done");
    }
    public void addF(Function<String,String> f){
        this.f = f;
    }
}

public class bClass{
    public String func(String str){
        System.out.println(str);
        //Call doSomething();
        //Can I do that? 
    }

    public static void main(String[] args){
        aClass a = new aClass();
        a.addF(this::func);
    }
}
  • 1
    What do you need /trying to achieve? – Jimmy T. Nov 07 '17 at 07:15
  • Hint: you are talking about methods, not fields. And no, the essence of the private keyword is to restrict visibility to the scope of the enclosing class. – GhostCat Nov 07 '17 at 07:19
  • And then read about Java naming conventions. Class names start UpperCase - always. – GhostCat Nov 07 '17 at 07:20
  • I want to nest doSomething() into f(), but f() is defined external so I can not solve. Do you have any idea? Sorry about my English. I'm so bad at English. – phong nguyen Nov 07 '17 at 07:21
  • No, Its not my question. As you can see, f() was declare inside aClass and define code in bClass – phong nguyen Nov 07 '17 at 07:27
  • You can write a method in `aClass` that first calls `f.apply()` and then calls `doSomething()`. Is that what you need? P.S., you are passing a bad method reference to `a.addF()`. – Eran Nov 07 '17 at 07:29
  • @Eran Thanks for support. I got it now. – phong nguyen Nov 07 '17 at 07:33

0 Answers0