Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with basic syntax

 
JR Pearson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... I see EFH and I are using the Sandwich Pattern in responding to this post. :roll:
 
JR Pearson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic