I'm not 100% sure what you mean. Are you talking about how to execute your app, or how to write the Main method itself? If you create a Console Application project then a Program class with a Main method that will be the entry point for the app. Does that cover it?
main()
, although most of the Java specific IDE's try to stop you from doing this. Without an IDE and doing everything command line, this works without issues:PS E:\z\test> cat A.java
public class A {
public static void main(String[] args){
System.out.println("A");
}
}
PS E:\z\test> cat B.java
public class B {
public static void main(String[] args){
System.out.println("B");
}
}
PS E:\z\test> javac A.java
PS E:\z\test> javac B.java
PS E:\z\test> jar cvf test.jar A.class B.class
added manifest
adding: A.class(in = 393) (out= 271)(deflated 31%)
adding: B.class(in = 393) (out= 271)(deflated 31%)
PS E:\z\test>
Main()
. (I've heard there is a way to override this, but one has to bend over backwards to do this.)main()
. It also prevents the student from looking back at previous exercises if they have thrown it away.I just tested this and you're correct, except for the "bend over backwards" bit. I just created a C# WinForms app and then created a copy of the Program.cs file, which contains the Program class, which contains the Main method. I changed the name of the copied file to Program2.cs and the class to Program2. I tried to build and it did indeed complain that I had multiple Main methods. I then went to the Application page of the project properties, where the Startup object field was (Not set). I selected one of the types containing a Main method from the drop-down and was then able to build successfully. According to the error message, that field sets the "/main" compilation option to specify the entry point.This is different in the .NET Framework: each assembly can only have at most oneMain()
. (I've heard there is a way to override this, but one has to bend over backwards to do this.)
using System;
namespace FootballLeague
{
class MainMethod
{
static void Main(string[] args)
{
Program myProgram = new Program();
myProgram.MainProgram();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace FootballLeague
{
class Program
{
public void MainProgram()
{
Questionaire userQuestions = new Questionaire();
userQuestions.WelcomeUser();
userQuestions.UserQuestionaire();
}
}
}
MainProgram
class, or that you are trying to avoid static methods, then the extra indirection only serves to confuse things.using System;
using System.Collections.Generic;
using System.Text;
namespace FootballLeague
{
class Program
{
public void MainProgram()
{
Questionaire userQuestions = new Questionaire();
userQuestions.WelcomeUser();
userQuestions.UserQuestionaire();
}
static void Main(string[] args) => (new Program()).MainProgram();
}
}
public static void Main()
{
// lots and lots of code here composed of:
// first modular part
// second modular part
// third modular part
// :
// n-th modular part
}
public static void Main()
{
FirstModularPart();
SecondModularPart();
ThirdModularPart();
:
NthModularPart();
}
private static void FirstModularPart()
{
// modular section of code here
}
private static void SecondModularPart()
{
// modular section of code here
}
private static void ThirdModularPart()
{
// modular section of code here
}
:
private static void NthModularPart()
{
// modular section of code here
}
public static void Main()
{
(new Program()).Run();
}
void Run()
{
FirstModularPart();
SecondModularPart();
ThirdModularPart();
:
NthModularPart();
}
void FirstModularPart()
{
// modular section of code here
}
void SecondModularPart()
{
// modular section of code here
}
void ThirdModularPart()
{
// modular section of code here
}
:
void NthModularPart()
{
// modular section of code here
}
private
when it is implied. I know some users prefer to see it explicitly written, and Visual Studio's StyleCop suggests that it also be written.)I avoid using static methods within classes