How to write Java Program

In order to write Java code, use any of the IDE’s available. Create a project say “sample” and then create a class say “DemoPrint” in IDE. By convention the class names should be nouns with each internal word capitalized.

In java, code is written inside a class. Let's say we want to print a statement say "Hello Joe, How are you?" using Java program.

package sample;
public class DemoPrint
 public static void main(String [] args)
     System.out.println("Hello Joe, How are you?");




Steps to write a Java Program

  • Begin with writing a package name. A package is a namespace that organizes different classes and interfaces. It’s like different folders in our computer. In this case, we created a project named “sample” so package name is sample. The syntax is:
    • package sample;
  • public class DemoPrint
    • Declare a public class. public is a keyword by which we can access all the members of the class from other classes. class is a blueprint that has data members and member functions. Write public keyword and then classname. By convention class names should be nouns with first letter of each internal word capitalized.
  • public static void main(String [] args)
    • public is an access specifier that allows the main method to be accessible everywhere.
    • static is a access modifier that allows to call the method directly by classname without actually creating a object of it.
    • void is a return type that does not returns any value.
    • main is a method name which is entry point for the JVM.
    • String [] args is a string array to pass arguments. I can write anything as arguments like String [] joe.
  • System.out.println(“Hello Joe, How are you?”)
    • System is a final class that provides input/output facilities.
    • out is the static member of the class that gets mapped with the standard output console of the host.
    • println is a method that prints the statement and adds a newline.


Rate this post


So empty here ... leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *