aspose file tools*
The moose likes Beginning Java and the fly likes Is it possible to write a java program without using class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is it possible to write a java program without using class?" Watch "Is it possible to write a java program without using class?" New topic
Author

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

Rajasekhar Eega
Greenhorn

Joined: Jul 31, 2012
Posts: 11

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

Joined: Jan 10, 2002
Posts: 61202
    
  66

An enum is a class.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

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?


Junilu - [How to Ask Questions] [How to Answer Questions]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61202
    
  66

I'm guessing that it was someone's idea of a "clever" interview question.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14146
    
  18



Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rajasekhar Eega
Greenhorn

Joined: Jul 31, 2012
Posts: 11
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

Joined: Aug 16, 2005
Posts: 14146
    
  18

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

Joined: Aug 15, 2011
Posts: 35
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

Joined: Aug 16, 2005
Posts: 14146
    
  18

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

Joined: Mar 17, 2011
Posts: 7779
    
  21

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to write a java program without using class?