Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
emile
7,980 PointsWhen and when not using main method in Java
I'm having trouble understanding the use of main method in Java. In Java Objects/Harnessing the Power of Objects there is a PezDispenser.java file and an already made 'class' file (same for Example file). But when I delete the class file and compile the 'java' again, i get an error: I have to define the 'main method'. So why doesn't it run anymore if I compile it myself but it does run with the original files?
1 Answer
jcorum
71,830 PointsThe basic thing to remember is that there is a big difference between programs and classes. Both start off the same:
public class ClassName { . . . }
but programs have a main() method, classes do not. You can compile and run programs. You can compile classes, but you can't run them.
PezDispenser is a class:
public class PezDispenser {
public static final int MAX_PEZ = 12;
private String mCharacterName; private int mPezCount;
public PezDispenser(String characterName){
mCharacterName = characterName; mPezCount = 0;
}
public boolean isEmpty(){
return mPezCount == 0;
}
. . .
}
There is no main() method. It is not runnable.
Example is a program:
public class Example {
public static void main(String[] args) {
// Your amazing code goes here...
System.out.println("We are making a new Pez Dispenser.");
PezDispenser dispenser = new PezDispenser ("Yoda");
. . .
}
}
You can run this program. In the main() method a PezDispenser object named dispenser is being created. This line of code would cause an error if the PezDispenser class wasn't available (in the same directory, ...).
In order to call a method that is in a class you need to create an object or instance of the class first, and then call the method on the object.
PezDispenser p = new PezDispenser("...");
c.isEmpty(); //assumes PezDispenser has a isEmpty() method.
Here the general rule for calling methods is objectName.methodName
Some folk also made a distinction between programs, classes and modules, with the latter being classes with static methods and no main() method. An example of a "module", in this sense, is the Math class. The general rule for calling methods on modules is ClassName.methodName. E.g., Math.sqrt(24.0)
Hope this helps.
jcorum
71,830 Pointsjcorum
71,830 PointsOne additional comment. The above is the simplified version. It gets a bit more complex. But the videos don't go there. E.g., you can have a main() method in a class. You can have inner classes, etc. But like I said, that's a story for later.
emile
7,980 Pointsemile
7,980 PointsThanks, this really did explain the difference