jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Help with basic syntax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with basic syntax" Watch "Help with basic syntax" New topic
Author

Help with basic syntax

JR Pearson
Greenhorn

Joined: Jul 19, 2006
Posts: 5
Hi everyone! I'm just getting started learning Java, and I have what is probably a very basic syntax question.

"public static void main(String[] args)"

What does this line do? I understand the "public static void" part (mostly), and I'm assuming that "main" is the method name. But I'm not sure what this line is actually "doing"

I was trying to write a VERY basic (about 6 lines) program testing the properties of "int", "double", and the process of casting data types. It would compile without that line, but trying to run the program after the fact would give me the error.

"exception in thread "main" java.lang.NoSuchMethodError: main"

I've also noticed that applets don't seem to need this line. Is that because the applet already contains the necessary "main" information?

Is this something that I should even be concerned with at this stage in my learning? What I mean is; should I just accept for now that this needs to be at the start of every piece of code I write, and as I learn more I will understand why it's used and how to change it to make it do what I want?

Thanks a bunch everyone! Probably going to be talking with you fine folks quite often over the next few months...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"is misunderstood,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your display name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. You can change your display name here. Thanks!

Now, as to your question: when you run the "java.exe" program, you tell it the name of a Java class to execute. It looks in that class for a method named "main" that is public and static, returns void (nothing) and accepts an array of Strings for arguments (String[]); in other words, it looks for a method declared as

public static void main(String[] argv) { ...

and it calls that method, so whatever code is in there gets executed. Learning what each little bit of this declaration means will happen during the first chapter or two of whatever Java book you're using, so I wouldn't worry about it until you get to it.

Applets don't have a "main", but they have "init()", "start()", "stop()", and "destroy()". Whereas java.exe looks for main(), appletviewer.exe looks for these other four methods (that's a tiny bit of a white lie; the explanation is more complex than that, but it's good enough for now.)


[Jess in Action][AskingGoodQuestions]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Hmmm, where to start...

Indeed, that's the question. When you run a program, it needs an "entry point." The main method is what's called when a program is run from the command line. Without a main method, you will get the NoSuchMethodError when you try to run the program.

As of version 1.4, main() methods are required to be public in order to run from the command line. As an entry point, they must be static (accessible prior to instantiation), and their return type must be void. The argument type must be String[], identified by any legal identifier. So...

public static void main(String[] args) {...}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Hmmm... I see EFH and I are using the Sandwich Pattern in responding to this post. :roll:
JR Pearson
Greenhorn

Joined: Jul 19, 2006
Posts: 5
Wow, thanks! (Changed the name BTW, sorry 'bout that! I tried a real name at registration, but it didn't like it, so I went with the old handle).

I think I'm following. "Main" is the method, everything inside the {}'s immediatly following it is what's going to be run.

I have another question that will be coming in the very near future about what happens when I need more than one method, but I'll hold that one until the time comes when I can understand the answer! For now, just getting the basics of data types and syntax is my biggest concern. I'll work out the details later.

Thanks everyone for the super fast responses!!!

edit--you guys rock...
[ July 19, 2006: Message edited by: JR Pearson ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by JR Pearson:
...I have another question that will be coming in the very near future about what happens when I need more than one method...

Keep in mind that methods are called from within the bodies of other methods. So with main as a starting point...
JR Pearson
Greenhorn

Joined: Jul 19, 2006
Posts: 5
Keep in mind that methods are called from within the bodies of other methods. So with main as a starting point...


I think I see where you're going with that...but I'm going to leave it alone for now. So far as I'm concerned, that's advanced stuff! I'll get there eventually, but for now it's back to strings, basic variable types, and arrays are coming next (I would probably expect a question on those soon too!)

Thanks again everyone!
 
Don't get me started about those stupid light bulbs.
 
subject: Help with basic syntax