• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67665
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An enum is a class.
 
Sheriff
Posts: 17092
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67665
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm guessing that it was someone's idea of a "clever" interview question.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Rajasekhar Eega
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Message for you sir! I think it is a tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic