Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question