• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Is main being optional in Python different from main not being required in Java POJOs

 
Ranch Foreman
Posts: 2390
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read that in Python main method is optional. Even in Java, main is not required if the class is a POJO. Is main not being mandatory in python any different from the case of java where it is not required in case of POJOs?
 
author & internet detective
Posts: 40487
823
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A POJO isn't a program though. It's a class. You can't run a class which is why it doesn't have a main method.
 
Monica Shiralkar
Ranch Foreman
Posts: 2390
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
POJO isn't a program and has to be imported and then used.
A python file which imported is also imported and used. Java program has main method.Python program may or may not have main.It can run code line by line from the top too.
 
Saloon Keeper
Posts: 23282
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:I read that in Python main method is optional. Even in Java, main is not required if the class is a POJO. Is main not being mandatory in python any different from the case of java where it is not required in case of POJOs?



POJO has nothing to do with it. Any Java class can have a main method. Granted, a class isn't operating as a POJO when it's executing a main method, but a main method can always instantiate its class and operate the instance as a POJO. Such things have been done for testing purposes, for example.

On the other hand, the concept of a main method doesn't exist in Python. Python just takes whatever Python file you point it at and starts executing top-to-bottom. The conventions I've mentioned earlier are merely conventions. A Python file need not declare any methods, functions, or classes.
 
Monica Shiralkar
Ranch Foreman
Posts: 2390
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. In core Java application atleast somewhere an entry method main has to be there whereas this is not mandatory in Python.
 
Ranch Foreman
Posts: 235
8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's trivia, but now that interfaces can have methods, even interfaces can have runnable main() methods in Java -- for some reason some people love pointing this out, it works.

The important difference is that for simple programs, there doesn't have to be any methods at all in Python, they are as simple as .bat files in MS_DOS in that way...
 
Monica Shiralkar
Ranch Foreman
Posts: 2390
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
Saloon Keeper
Posts: 12734
277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't confuse Java the language with java the command line tool. If you use jshell instead of java, you don't need a main method.

Heck, I recall writing a shell script in Java once, just to prove a point.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic