Zack
Zack

Reputation: 671

Why do some classes require main methods and others do not?

I'm currently enrolled in an online Java class, and my instructor has led me to believe that all Java classes must have a main method

ie.

public class
{
public static void main(String[] args)
}

However, we've just reached a unit on cross referencing classes in other files where this is not the case.

Ex.

public class Pie
{

        // declare variables to be called in separate file
        String type;
        int diameter;
        float radius;

}

Pie can then be referenced in a fashion such as:

Pie newPie = new Pie();

System.out.println("What type of pie will you be eating today?");
        newPie.type = in.readLine();
        System.out.println("Ah. " + newPie.type + ". Excellent choice.\n");

And this works fine. However the explanation behind why this functions properly eludes me. Can anyone please explain?

Upvotes: 6

Views: 7132

Answers (5)

Christina
Christina

Reputation: 138

A Java class and a Java program are two different things.

A Java program contains one or more Java classes, and each class can contain methods and variables. The main method is a special static method that Java recognizes as an entry point in a program (you can think of the main method as a blueprint or set of directions for a program, versus a class being a blueprint of an object like a Pie).

So to answer your question, there's been a misunderstanding. A Java program needs a main method, and a Java class does not necessarily need a main method.

Upvotes: 2

npinti
npinti

Reputation: 52185

Every Java program (which is in turn, built up from one or more Java classes) requires a Main method. The purpose of this special method is to serve as an entry point to your program so that your program can be executed. More information can be found in this page.

In your Pie example, what happens is that when you run your application, the main method will be the first thing which will be called. Once that it will be called, it will create a new Object, named newPie using the Pie template (class) and so on.

Just as extra information, if you are using an IDE, if you add a main method in your Pie class with the given signature: public static void main(String[] args), the next time you run your program the IDE will ask you to select your entry point since it will have now found two entry points. Once that you will have made your selection, the IDE will make the necessary configurations so that the entry point of your application is noted.

Upvotes: 8

eatSleepCode
eatSleepCode

Reputation: 4637

It is not necessary to have main method in every java class main method is the entry point of java application. There can be a class without main method.

Upvotes: 1

Mostafa Jamareh
Mostafa Jamareh

Reputation: 1439

they are different ,

typically you created class in your app to store some data

but there is another class that is your main class which will launch your app and your app start will be there and call another class

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

my instructor has led me to believe that all Java classes must have a main method

this is wrong. It is not necessary for all the classes to have a main method. main method is used as an entry point for java applications. So once you have entered the java code using main method of a single class you can call other classes code form there.

A better statement would be:

In the Java programming language, every application must contain a main method, which serves as the entry point of application.

Learn more about main method here:

http://docs.oracle.com/javase/tutorial/getStarted/application/index.html?utm_source=twitterfeed&utm_medium=twitter#MAIN

Upvotes: 4

Related Questions