Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Running an application from an interface  RSS feed

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering...
You can now declare static methods in interfaces.
I was curious whether it is possible to run an application from an interface. It turns out it is!That works and prints Hello world!
What do you think? Have you ever seen an application started like this?

You may need to use javac -encoding utf8 Temp.java to compile this.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When they introduced default methods in Java8, they blurred the distinction between an interface and an abstract class. So that code is behaving almost as if it were an abstract class. Let's try a bit of fiddling with it. Let's add an abstract method to it
void foo(int i);
…and lo and behold, it still compiles and prints Hello World.

If you change interface to abstract class in that code, it fails to compile because the abstract method has to be declared abstract explicitly, and fails to run because the main method must be declared public. The public access and abstract‑ness are implicit in an interface. Correcting those two things allows the class to compile and display Hello World.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:. . . Have you ever seen an application started like this? . . .
Sounds like a party trick, like the old question on how to execute Java® code without a main method. What is the character for the name of the main method parameter?
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What is the character for the name of the main method parameter?

This is the symbol for the rupee, the currency of India and Sri Lanka.
Perfectly valid Java identifier.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which rupee? Is it \u0bf9?
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Is it \u0bf9?

Yes, it is. You seem to care too much about this character.

I just wanted to use some valid strange-looking identifier so it would be clear that this should not be copied and reused.

As you said, it is a trick.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:. . . You seem to care too much about this character. . . .
No, it was simply a mystery I wanted to clear up.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!