• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it possible to write a java program without using class?

 
Rajasekhar Eega
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

I listened somewhere with the help of enum we can write a java program without using class.
Is it true?

If it is true. Please help me how it is possible?

Thank you
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An enum is a class.
 
Junilu Lacar
Bartender
Posts: 7602
54
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may also be possible to drink milk through your nose but why in the world would you want to do that? Just to see if you can? Why not put your time and effort into learning how to do something useful with Java instead?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing that it was someone's idea of a "clever" interview question.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rajasekhar Eega
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just i got it. But I am unable to understand the reason. Why it is working?

Please help me.

public enum AAA {

AAA;

public static void main(final String[] args) {
System.out.println("H");
}

}

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Bear said, an enum is just like a special kind of class. You can add arbitrary methods to enums. The main() method is just a method that you can add to an enum.

As Junilu says, this is a funny trick, but not useful for anything in practice.
 
John McClellan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, you can't really make a Java program completely without the use of a class. Java is a pure object-oriented language, and every last thing that goes on in it happens within a class (counting stuff that uses different keywords).

C++, of course, is a hybrid language between the procedural and OO paradigms. Its main function goes outside of a class (still counting stuff that uses different keywords, but not including enums in this case), and classes are basically optional.

I ADAMANTLY disagree with the notion that this question is irrelevant. To be good at programming in a certain language, you need to have a pretty solid idea of what that language's rules are. Even obscure, obviously, and seemingly irrelevant knowledge of the rules can come in handy. I mean, Java programmers need to know that the main function has to be put in a class of some sort. He/she's asking for clarity on that point, which is VERY relevant.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John McClellan wrote:Java is a pure object-oriented language, ...

The question "Is Java a pure OO language?" is one of those frequently asked questions here (presumably because it's often asked in job interviews or for school exams) and the answer to the question is not as clear cut as you think. There isn't an exact definition of what "pure OO language" means. Some people say that Java is not a pure OO language because not everything is an object in Java (primitives are not objects).
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John McClellan wrote:To be good at programming in a certain language, you need to have a pretty solid idea of what that language's rules are. Even obscure, obviously, and seemingly irrelevant knowledge of the rules can come in handy. I mean, Java programmers need to know that the main function has to be put in a class of some sort. He/she's asking for clarity on that point, which is VERY relevant.

And I'm afraid I ADAMANTLY disagree with:
(a) That statement.
(b) The idea that a beginner is likely to be a good arbiter of what's relevant or useful.

I've spent too long answering questions from people in their first weeks/months of the Java learning process who want to know exactly where objects are stored in memory, or when to call the garbage collector, or how hashmaps work internally, or why stupid Java does such-and-such to have a lot of faith in their ability to discern what IS and what is NOT relevant.

Many questions that aren't relevant when you're 6 weeks into a course may become relevant later on, when you have the requisite knowledge to digest the answers. Based on what I've read in this thread, I'm with Bear and Junilu.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic