P
P
P7472020-05-06 12:56:28
Java
P747, 2020-05-06 12:56:28

Java - calling a non-static method from a static one similar to PHP?

Good afternoon!

Is it possible in Java to call a non-static method from a static one, without using the name of the current class when creating an object, just like in PHP - "new self ()"? That is, in PHP there will be such a code:

<?php

class Test {
    
    public function printPhrase() {
        echo "Hello Word!\r\n";
    }
    
    public static function main() {
        $staticThis = new self();
        $staticThis->printPhrase();
    }
}

Test::main();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Kuznetsov, 2020-05-06
@P747

You can't do that. In Java, you cannot access non-static class members inside a static context. If you rewrite your example in Java, it will not compile.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question