• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

One main, multiple classes?

 
Greenhorn
Posts: 1
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, all!

I'm new to this forum and to Java. I was reading the book, "Head First Java" and it was talking about how there may be multiple classes in a large application, but there will be only one main method. I'm sure the book will cover this later, but I'm impatient. My question is, how does it work that way? How can you have multiple classes, but only one main method?


Thank you.
 
author & internet detective
Posts: 41860
908
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
Welcome to CodeRanch!

A main method is a starting point. The code in the main method can create other objects. In a game of chess, we might have a main method that knows how to play chess. It would create a gameboard class. And each chess piece. And a score. Etc.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To give you a visual example:

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally speaking, you only do have one main method that serves as the initial entry point to the application. Think of it as the main door to the house. Most houses only have one main or front door. Strictly speaking however, any or all of the classes that are used in an application can have a main method. It doesn't make much sense to do that but there's nothing that will prevent from doing it. You can even call the main method of any class that has one when your application is already running but again it doesn't make much sense to do that.
 
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

Junilu Lacar wrote:Think of it as the main door to the house...


I rather like that analogy: A Java program is a house, and classes are the rooms... Neat.

@Steven: Another thing that's worth remembering: in Java, classes are components; but the actual unit of execution (ie, a "program") is normally a jar, which can contain many classes. However, it only has one start point (stored in its manifest), so any given jar only needs one class with a main() method.

However, as Junilu says, there's nothing to stop you putting it in more than one class if you want.

I don't want to overload you too much at this stage, but one other thing to remember about main() is that it's a static method.
And static methods can only call other static methods, which leads a lot of beginners to start making everything static.

My advice: DON'T - It's a very restrictive way to program.

Java works best when you have objects and use their instance (ie, non-static) methods, so in your main() method, create an object as fast as you can and use its methods.

You can find one way to do this in the MainIsAPain page; but it's just one way, there are lots of others.

HIH

Winston
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some people write a main method in every class, and use it to test that class out. After the testing, you can inactivate the main method by changing its access to private (there are lots of other ways to do that).
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Some people write a main method in every class, and use it to test that class out.


I was hoping that genie would stay in the bottle
but yes, that's usually the case when you see multiple main methods in one application. Prefer using JUnit or some other testing framework though.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I said, you need to inactivate the main methods when you have finished the testing. Changing their access to private is effective.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Changing their access to private is effective.


Only in very limited sense. If you need to test again or add a test, you'd need to change access back to public. Multiply that effort N times for however many classes you've used this strategy and this approach gets old really fast. Also, you have all these main methods that become part of your application. With testing frameworks like JUnit, the test code stays separate from the production code. And then there's the integration with tools like Maven that you'll lose with main as a test driver. Having main methods as test drivers is only good for one-off cases where you won't ever have to come back to maintain or run the tests again once you're done with them.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree. I would do better to learn a test program.
 
Onion rings are vegetable donuts. Taste this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic