Author | Message | Time |
---|---|---|
j0k3r | [code] public class BasicsDemo { public static void main(String[] args) { int sum = 0; for (int current = 1; current <= 10; current++) { sum += current; } System.out.println("Sum = " + sum); } } [/code] Why is the entire program incased in a class? And what determines the arguments for public class void main()? | November 23, 2003, 1:43 PM |
iago | Ok, time to answer this!! Ok, every file in Java has a public class, with the same name as it's file (like, iago.java would have public class iago). This class can be used from anywhere in the folder (like, in /projects/iago/iago.java, /projects/iago/joe.java could also use class iago). When you run a Java program (on commandline, by typing "java iago [args]"), it will call class.main(args). main() has to be static, so it can be called without an instance of the class being made, public so it can be called from outside, and it returns nothing. In Java, everything is in a class. [Edit] Personally, I *only* use public classes. I make a new file whenever I need a new class. See my Java Bot. :) Also, you should use a more descriptive topic name :P | November 23, 2003, 1:47 PM |
St0rm.iD | [quote author=iago link=board=34;threadid=3790;start=0#msg30884 date=1069595255] Ok, time to answer this!! Ok, every file in Java has a public class, with the same name as it's file (like, iago.java would have public class iago). This class can be used from anywhere in the folder (like, in /projects/iago/iago.java, /projects/iago/joe.java could also use class iago). When you run a Java program (on commandline, by typing "java iago [args]"), it will call class.main(args). main() has to be static, so it can be called without an instance of the class being made, public so it can be called from outside, and it returns nothing. In Java, everything is in a class. [Edit] Personally, I *only* use public classes. I make a new file whenever I need a new class. See my Java Bot. :) Also, you should use a more descriptive topic name :P [/quote] Not neccessarily. A file doesn't need to have a public class in it. It can just be a normal class, which has package access. This is useful when writing libraries. And not everything is a class...everything except the primitive data types (int boolean etc). | November 23, 2003, 3:11 PM |
Kp | As iago tried to hint at, a public class must be in a file of the same name. However, as St0rm mentioned, the reverse is not true: a file can contain zero or more non-public classes of any name whatsoever. For some design decision which I fail to understand, it is a compile time error to try to put a public class in a file which has a nonmatching name. Nonpublic classes are particularly useful/attractive when you want to have "helper objects." For instance, if you decided to write your own LinkedList implementation, it'd be logical to have the nodes of that list be a nonpublic class since it really doesn't make much sense to have random functions outside your linked list able to create linked list nodes. :) Also, to clarify what iago said, when you do java X, the java VM looks for X.class in your classpath. Class X must have a [u]public static void main(String[])[/u] function, or the program cannot be executed. Note that it is theoretically possible to have multiple entrypoints for your program by having several different classes, each with their own main(String[]) function. Which one you reference on the command line controls which entry point you get. | November 23, 2003, 5:48 PM |
j0k3r | [quote author=iago link=board=34;threadid=3790;start=0#msg30884 date=1069595255] In Java, everything is in a class. [/quote] He said everything is IN a class, not everything IS a class, but thanks, it clears some of the smoke I'll have to read up on it some more. What determines the arguments for public class void main()? | November 23, 2003, 6:23 PM |
xsemaphorex | <code> //Filename Foo.java public class Foo { public static void main(String[] myvar) { } } </code> main(String[] data) is the application entry point for the class <insert class name here> when you run the java program using the command line: java Foo The String[] parameter is simply the commands after Foo on the command line starting at 0 being the first argument. (Note that -1 is not accessible and will throw an ArrayIndexOutOfBounds Exception if you try accessing it.) For example: java Foo my name is mud will send the parameter String[] data = {"my", "name", "is", "mud"}. and java Foo "my name is" mud will send the parameter String[] data = {"my name is", "mud"} There's much more. In java every single member belongs to a class; be it an instance of that class or the class itself. (ie static members). There are no 'classless' members. This lends to code that is much cleaner and simpler to understand (in theory of course.. as I work with a bunch of towell-head indians that code as if they took a single prep class and then lied on their resume concerning their experience and education from some obscure university like calcutta tech... but HEY I'm not bitter :P) I'm sure though that if you have more questions... you'll ask.. :) | November 23, 2003, 7:04 PM |
Kp | [quote author=j0k3r link=board=34;threadid=3790;start=0#msg30919 date=1069611803] What determines the arguments for public class void main()? [/quote] The user. If you were to do java MyApp -a 1 2 3, main would receive a String[] with 4 elements: "-a", "1", "2", and "3". Anything after the class name on the command line becomes an argument to the entry point function. | November 23, 2003, 7:13 PM |
j0k3r | [quote author=j0k3r link=board=34;threadid=3790;start=0#msg30882 date=1069595018] [code] public static void main(String[] args) { [/code] [/quote] Ok, that leads me to asking what you would do with it and where are the strings stored? Do you have to give it a variable to store it in? Are the arguments mandatory? | November 23, 2003, 9:55 PM |
iago | It's just like in C int main(int argc, char *argv[]) since it's all taken care of for you. | November 23, 2003, 11:00 PM |
Skywing | [quote author=iago link=board=34;threadid=3790;start=0#msg30960 date=1069628438] It's just like in C int main(int argc, char *argv[]) since it's all taken care of for you. [/quote] [code] Not quite. In C, argv[0] is typically the executable name, and argv[1] is the first command-line argument. In Java, args[0] is the first command-line argument.[/code] Edit: Enclosed in code tags due to an annoying forum bug that breaks subscript notation. | November 23, 2003, 11:25 PM |
j0k3r | [quote author=iago link=board=34;threadid=3790;start=0#msg30960 date=1069628438] It's just like in C int main(int argc, char *argv[]) since it's all taken care of for you. [/quote] I don't know C. | November 24, 2003, 12:41 AM |
iago | [quote author=Skywing link=board=34;threadid=3790;start=0#msg30963 date=1069629941] [quote author=iago link=board=34;threadid=3790;start=0#msg30960 date=1069628438] It's just like in C int main(int argc, char *argv[]) since it's all taken care of for you. [/quote] [code] Not quite. In C, argv[0] is typically the executable name, and argv[1] is the first command-line argument. In Java, args[0] is the first command-line argument.[/code] Edit: Enclosed in code tags due to an annoying forum bug that breaks subscript notation. [/quote] I actually didn't mean it that way, I meant that the OS fills it in :P | November 24, 2003, 8:30 AM |
St0rm.iD | [quote author=j0k3r link=board=34;threadid=3790;start=0#msg30919 date=1069611803] [quote author=iago link=board=34;threadid=3790;start=0#msg30884 date=1069595255] In Java, everything is in a class. [/quote] He said everything is IN a class, not everything IS a class, but thanks, it clears some of the smoke I'll have to read up on it some more. What determines the arguments for public class void main()? [/quote] He changed his post. | November 24, 2003, 12:05 PM |
j0k3r | [quote author=iago link=board=34;threadid=3790;start=0#msg31056 date=1069662632] [quote author=Skywing link=board=34;threadid=3790;start=0#msg30963 date=1069629941] [quote author=iago link=board=34;threadid=3790;start=0#msg30960 date=1069628438] It's just like in C int main(int argc, char *argv[]) since it's all taken care of for you. [/quote] [code] Not quite. In C, argv[0] is typically the executable name, and argv[1] is the first command-line argument. In Java, args[0] is the first command-line argument.[/code] Edit: Enclosed in code tags due to an annoying forum bug that breaks subscript notation. [/quote] I actually didn't mean it that way, I meant that the OS fills it in :P [/quote] So I can just leave it blank and not worry about it? | November 24, 2003, 12:31 PM |
iago | [quote author=St0rm.iD link=board=34;threadid=3790;start=0#msg31077 date=1069675519] [quote author=j0k3r link=board=34;threadid=3790;start=0#msg30919 date=1069611803] [quote author=iago link=board=34;threadid=3790;start=0#msg30884 date=1069595255] In Java, everything is in a class. [/quote] He said everything is IN a class, not everything IS a class, but thanks, it clears some of the smoke I'll have to read up on it some more. What determines the arguments for public class void main()? [/quote] He changed his post. [/quote] If you mean me, that's a lie, I didn't change that part of my post. And no, Java doesnt' let you leave it blank, it ONLY allows public static void main(String[] args); | November 24, 2003, 2:37 PM |
j0k3r | Perfect, less confusion. | November 25, 2003, 1:19 AM |