Share with others

Public and Private Keywords Java
The public keyword is an access specifier.
Determines how the other parts of the program can access the members of the class.
When a class member is preceded by public, then that member can be access by code outside the class in which it is declared. 

The opposite of public is private.
Prevents a member from being used by code defined outside of its class.

In our program, main() must be declared as public since it must be called by code outside of its class when the program is started.

The keyword static allows main() to be called before an object of the class has been created.
This is necessary since main() is called before an object of the class has been created.

The keyword void tells the compiler that main() does not return a value.
Methods may also return values.

As stated, main() is the method called when a Java application begins.

Any information that you need to pass to a method is received by variables specified within the set of parentheses that follow the name of the method.
These variables are called parameters.
If no parameters are required for a given method, you still need to include the empty parentheses.

The next line
System.out.println (“Java Language”);
Outputs the string “Java Language” followed by  a new line on the screen.

Output is accomplished by the built-in println( ) method.
println( ) displays the string which is passed to it. println( ) can be used to display other types of information as well.

The line begins with System.out.
System is a pre-defined class that provides access to the system out is the output stream that is connected to the console.

System.out is an object that encapsulates console output.
Console input and output is not used frequently in real-world Java programs and applets. Since most application are windowed and graphical in nature, console I/O is used for simple utility programs and demonstration programs.

All statements in Java end with a semicolon.
Notice that the println( ) statement ends with a semicolon.

Java is case sensitive.
If you accidentally type Main instead of main, the program will be incorrect.
Although the Java compiler will compile classes that do not contain a main() method, it has no way to execute them.

 


Share with others