• Post Reply Bookmark Topic Watch Topic
  • New Topic

One main, multiple classes?  RSS feed

 
Steven Peters
Greenhorn
Posts: 1
Chrome Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To give you a visual example:

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • 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: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • 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: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree. I would do better to learn a test program.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!