0

Is it possible to write primitive function in ? Something like this:

void x() {
    System.out.printf("ok");
}

Like this in :

function x() {
    console.log('ok');
}
cn007b
  • 16,596
  • 7
  • 59
  • 74
  • 1
    You mean a standalone function, as opposed to a method? – khelwood Oct 23 '20 at 11:52
  • Add `static` to it? Or do you mean lambdas? – dan1st Oct 23 '20 at 11:53
  • @khelwood yes, function without class. – cn007b Oct 23 '20 at 11:53
  • Not really. You can put a static method inside a class. Or you can assign a lambda function to a local variable or a field. But it's still inside a class. Pretty much everything in Java is inside a class. – khelwood Oct 23 '20 at 11:53
  • @dan1st `static void x() { ...` -> `error: class, interface, or enum expected` – cn007b Oct 23 '20 at 11:54
  • What is your usecase, never had one so i am wondering, what you are doing. You need atleast a main class, inside of it you have then your main method and probably some more methods – Jarlik Stepsto Oct 23 '20 at 11:54
  • 1
    Each method needs to be defined in a class but methods can live without instances if they are `static`. What you want breaks the OOP concept of java. – dan1st Oct 23 '20 at 11:54

0 Answers0