I really like this explanation, it's very simple and to the point. But I hope you don't mind me going into a little more detail! :smile:
Class
Simply put; A class is a way of applying a structure to code, it stores Data & Methods as class members and allows the programmer to apply strict rules to each member. Classes can be instanced and thus allows them to be used & re-used in an application. This keeps code functional & tidy.
In the case of this class definition:
Hidden Content
You must
[To see links please register here]
or
[To see links please register here]
to view this content.
The class has been declared with the name "testclass" and has been defined as a 'Public' class.
This 'Public' Modifier entitles any other classes to access this class and any 'public' class members. Although since this is the main class, the 'public' modifier is not needed.
Class-Members
Hidden Content
You must
[To see links please register here]
or
[To see links please register here]
to view this content.
The above code demonstrates a class member being defined, this member has been defined with the following modifiers; public, static & void.
These modifiers are easily explained:
Public - This modifier entitles all classes (Parent, Sub & world) to access this member. (E.g. "testclass.main(MyStringArray);")
Static - This modifier means that only one instance of this class-member will be spawned and shared, regardless of how many instances of the class are created.
Void - This modifier means that that the class-member does not return anything. Had it been "public int main(String args[])" then an integer result would be expected when the member was called.
The main class-member also has a parameter: "String Args[]" - This means that when 'main' is called an
Array of the type string is parsed to it, allowing the method main to access this data. In this instance, the Array of Strings parsed to the method are Command Line Parameters. The Array can be accessed like so:
Hidden Content
You must
[To see links please register here]
or
[To see links please register here]
to view this content.
A Command-Line Parameter is data that is parsed to the program upon execution. For instance, if you compiled the above code and then executed "java AboveCode.java hello world" the variable "foo" would contain "hello" and the variable "doo" would contain "world".
Since our class is the only class within the application to contain "public static void main(String[] args)", on run-time our class will be instanced first and the the method 'main' will be executed.
Printing out
To help understand the class system, I will explain a little more about what we are doing when we use "System.out.println("");".
When we use System.out.println we are accessing 2 Public Classes and 2 Public Class-Members.
(class) System - This is the first public class we access.
(class) out - This is the first Public Class-Member we access and the Second Public Class (Class-Member of 'System')
(function) println - This is the second Public Class-Member we access, and this is what will print to the console (Class-Member of 'out')
So, when we break it down: 'println' is a class-member of 'out', and 'out is a class-member of 'System'.
The final code:
Hidden Content
You must
[To see links please register here]
or
[To see links please register here]
to view this content.
When we are parsing data to 'println' that is in raw form (compiled-constant as opposed to variable), we have to put that data in quotation marks. When parsing a variable, this is not necessary.
More about println
println is a something called an 'Overloaded Function', this means that there are many different instances of the function 'println' declared in the class 'out' each accepting different data as Parameters. This is why parsing an Integer or String to 'println' works without any errors.
I hope you don't mind me going into a little more detail, I think it will help people gain a further understanding once they have read your post.
Regards,
Logic.