Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Basics Getting Started with Java Introduction to Your Tools

what is the main method? example of main method. what is method declaration? and example, and what is args?, example.

anybody can clarify this please?.

2 Answers

Hudson Kim
Hudson Kim
2,965 Points

PLEASE read the whole thing to get a full understanding!

Main Method

 So basically the main method is just like a default method that is usually there if you want to use it but it is not required. In the main method, there is a parameter of an array of Strings, which means when you call the main method you have to put some input into it in the form of a list or array of Strings. For example: 

public static void main(String [] args) { .... (code you want to run in the method. For the bag object) }

In this example I created the main method and for the sake of time, lets just also say I created an object named bag.

 bag.main("Banna, Chocolate...")

This code will run the main method in which it takes in an array of Strings that the main method will do something with it such as count it or print it out.

Method Declaration:

Method declaration is basically the form in which you create a method (A method is basically a piece of code that will take in something and do something with it). SO to create a method you simply do the following:

public static void Count(String [] args) {


To create your method just simply put your access point first (public in the example above) and other modifiers you want to include (static, void) then the name you want to call it (Count), the parameters (An array or list of Strings named args mentioned above) and finally the magic or workings inside your method within the {};

Hope this helps and happy coding! -011100110001010

GhostIn Shell
GhostIn Shell
1,967 Points

I like to think of the main method in terms of a car. Its already built for you. It only needs something to power it. Gas and a Key. You tell it what to do by turning radio nobs and steering wheel. In terms of code you direct the main method by feeding it code. Without the main method you would not be able to tell the JVM(java virtual machine) what to do.

Args I think is the actual variable name. You could probably replace it with argument or the letter A. The String [] is the most important part, I think. It is difficult for me to explain as I am only a beginner myself. I think it accepts or passes information from the main method which is at a higher language.