โ“‚๏ธMain Method

What is a Method? (Interview Question)

A method defines an action that an object can perform. Think of it like a verb, just like objects can jump, sleep, etc.

Within a method's curly brackets is where you write the code for the action. This could be setting or retrieving variable values, performing calculations, printing outputs, etc.

When calling a method on an object, you use the dot notation (objectName.methodName). This tells the object to carry out what's defined in that method

๐Ÿค– The main() Method - Bringing Java Programs to Life!

The main() method is the heart and soul of a Java program โค๏ธ. It is the entry point that kickstarts a Java application's execution.

๐Ÿ“ Where to Find main()

The main() method resides inside a class and must be declared as:

public static void main(String[] args) {

} 

๐Ÿ—๏ธ Understanding the Syntax

  • public - main() is accessible from outside the class

  • static - main() can be called without creating an instance of the class

  • void - main() does not return anything

  • String[] args - main() accepts a string array as parameter for command line arguments

โ–ถ๏ธ Starting the Execution

The Java runtime calls the main() method to run your application. Code inside main() gets executed line by line.

๐Ÿ“ค Passing Inputs to main()

The args parameter of main() can be used to pass inputs and configure the application at runtime.

๐ŸŽ‰ Using main()

Typically, main() will:

  • Create objects

  • Call other methods

Last updated

Was this helpful?