Calling Non-Static Method With Double-Colon(::)

Use of double colons - difference between static and non-static method references

Averager averageCollect = zahlen.stream()
.collect(Averager::new, Averager::addcount, Averager::combine);

This is fine. It is equivalent to

Averager averageCollect = zahlen.stream()
.collect(() -> new Averager(),
(myAverager, n) -> myAverager.addcount(n),
(dst, src) -> dst.combine(src))

Remember every nonstatic method has a hidden this parameter. In this case it is (correctly) binding this to the first argument of the accumulator and combiner callbacks.

It will also work with static methods such as:

public static void addcount(Averager a, int i) {
a.total += i;
a.count++;
}
public static void combine(Averager dst, Averager src) {
dst.total += src.total;
dst.count += src.count;
}

which hopefully makes it clearer what is happening.

But there is no need to change the code.

How does java's double colon operator refer non-static method using class name

The Stream method sorted takes a Comparator<T> as parameter. This is a functional interface, so you can use a lambda expression or method reference as parameter that adheres to this interface. The method signature is as follows:

int compare(T var1, T var2);

So, for example, the following lambda expression is valid:

(MyClass a, MyClass b) -> a.compareTo(b)

A method reference can refer to static methods, but in this specific context, the type of the method reference can also replace the first parameter, so the following expression is equivalent to the above:

MyClass::compareTo

Similarly, you can do the following:

(String a, String b) -> a.equals(b)
String::equals

Or things like this:

(String a) -> "b".equals(a)
"b"::equals

Double Colon is working on non static function also

First thing is here that you are using the scope resolution operator which only works with static function . Either you have to make this function static using static keyword after public then you can call with its class name you would not have to make a object of that class or you can make a object like this

<?php 
$myobject = new myclass();
?>

And one major think the session_start(); function should be the first line of your webpage
because it sends the header info to the server. and server saves them.
otherwise it will be a problem that resending the header info using the php function because every htm always sends a http header info to server
so session_start should be the first line of any of the page

if this is relevant for you can mail me on info.gtensoft.in Senior Developer in Gtensoftwares www.gtensoft.in from new delji ,india

Double Colon lambda for two levels method invocation

What you are expecting is called chaining. Method references does not support chaining.

Read more about Method References.

PHP Call non static function with static way

You will get Strict standards: Non-static method test::hello() should not be called statically

and your Class declaration is not good, right way is this

class test
{
public function hello()
{
return "say hello";
}
}

class foo
{
public function __construct()
{
echo test::hello();
}
}

$foo = new foo();

Laravel uses Facade to give illusion that you are using static method, you can also use Facade to call method like test::hello();

Here is a good read on Facade



Related Topics



Leave a reply



Submit