Before explaining the above program, let’s first learn about few rules which you should be aware about while writing any Java program:
- Name of the Java program file should be similar to the name of the class in which “public static void main(String args)” method is used.
- Example– for the above program, the name of the java file should be “Example.java” (remember it’s case sensitive)
- METHOD: For every method name, the first word, first letter should be small and next word, first letter should be in Caps.
- For example– myShow(), input(), employeInput()
- CLASS: For every Class name, every word, first letter should be in Caps.
- For example– UseSystem, Automobile, Student
- Like in C++ we used to write
to get output on screen
- Here we write in java, to get output on the screen
- In any Identifier there should not be any space, special character (except $ or _) and first character should not be a number. (Identifiers are used for class names, variables names method names).
- Main– name of the method
- Void– data type of the method (as this method doesn’t return any value, hence it’s VOID)
- Static– Basically whenever we have to access a method of a class, we have to create an object of that class.
Now in case we want to access above methods, void setData() and getData(). Then we have to create an object first:
and then only we can access that method by using Dot operator (object_name.method_name)
But what happens by using keyword “STATIC” is, it allows a particular method to be called without having to instantiate a particular of that class (means by using it, we can access same method without making an object of that class).
- So in above program, we are accessing “Example” Class, “public static void main (String args)” , method without making any object of the class. Just because we have made the above method, “STATIC”.
4. System: It’s an predefined class
5. Out: It’s an object of “printStream” class (we will study in detail, about this class in coming posts)
6. Println: It’s a method of “printStream” class