First, the special part about: public static void main(String args) is that this is the method the jvm looks for to run a class from the command line. Other than that it is a method just like any other.
public: It has public access, meaning any user of the class has access to it.
static: The method is a member of the Class rather than a member of instances of the class. Static methods only have access to static variables within the class.
void: The return type is void (has no return).
main: The name of the method. As said before the jvm looks for the main method
String: This method takes an array of String objects. If you run this class from the command line using: java MyClass one two three : the String array will contain the Strings "one" "two" and "three".
args: The name of the String array. Can be anything, you could call it bob and it wouldn't matter. 'args' is just common practice.
The other thing to understand is this method is enclosed in a Class. The method is meant as an entry point and should not be used to place all your application logic in. Generally I feel really bad if I have much more than: MyClass class = new MyClass(); class.someMethod();
unless I'm parsing command line arguments. Even then I try and push the parsing into another method.
Thanks alot for the Info. I actually understand what it means now, thanks. But I do hava one more question....im trying to do a little program and its giving me errors but Im Doing it just like the book.
But it says this at the C:\> prompt...
Echo.java:12: cannot find symbol symbol : method create(java.io.inputstream) Location : java.util.Scanner Scanner scan = scan.nextline(); <--- (it has an upright arrow at that period.) Echo.java:16: cannot find symbol symbol : method nextline() Location : java.util.Scanner message = scan.nextline(); 2 Errors <----(it has the upright arrow by that period too.)
i dont know if it doesnt have that certain tool or (class) but i have no idea what to do...so if you could help me out...plz
[ EJFH: Fixed tags. ] [ February 02, 2005: Message edited by: Ernest Friedman-Hill ]
Your book may be a bit outdated; perhaps it covers a pre-release version of JDK 1.5. In any case, the Scanner class doesn't have a "create" method. It does have a constructor that takes an InputStream, though, so you could replace
Scanner scan = Scanner.create(System.in);
Scanner scan = new Scanner(System.in);
Similarly, there's no "nextline" method. There is, however, a "nextLine" method, with a capital "L". Making this small change should get your program to compile.
To get up-to-date information about the methods available in the Java 1.5 API, you should visit the API documentation. In fact, you should bookmark this page. In my opinion, learning how to navigate the API docs is a crucial skill when programming in Java.
ok im using the Scanner scan = scan.nextLine();.....it works and i thank you but how make it to where when they type in somethin.....i can make a response to that certain work or phrase?... like for instance..
Accidently pressed the tab and space bar....but as i was saying.. how do i make it to where if they put "yes" I could make the program say "that's good". And when they put "no" I could make it say "that sucks" thanks...(remeber this is for instance.)
For future reference, you can easily go back and edit your messages by clicking on the paper-and-pencil icon to the right of your user name. It's handy for situations like the one you encountered here.
As for the answer to your question, it sounds like you need an if statment of some kind. If you need the syntax, I'll let you look it up in your textbook or google for it. With the if statement, you typically need some kind of comparison. In this case, you should probably compare if two Strings are equal using the String.equal() method. Again, you may need to google for the exact syntax.
I've debated whether or not to put an example here, but I think I'll refrain this time. You can pick these things up from any decent Java text or tutorial. If you don't have a text to use as reference, I would suggest that you get one. There are several online texts, such as Bruce Eckel's Thinking in Java. Personally, I like having hard-copy instead of electronic books so I can write notes in the margin and highlite them. If you have the funds, I strongly suggest you look into it. If you need recommendations, use the Saloon's Search tool.
There is a lot of material available to help with Java syntax. From there you can go just about anywhere.
No, I've only read the first chapter or so in Bruce Eckel's book. My primary Java text is Core Java by Cay Horstmann and Gary Cornell. I found this to be a great book to start learning Java. However, I should point out that I already had a firm grasp on C++ at the time, so my background might be different than yours is.
As mentioned above, you need a web page to hold the applet. I don't have an example handy, but you should be able to google for one. During development, you may find it easiest to load the web page with the appletviewer program that comes with the JDK. You can run this from the command line with
Of course, you can replace "applet.html" with any file name as long as it is an HTML file.