Look into “HelloWorld”

Let’s look into “HelloWorld” code.

public class HelloWorld {
            public static void main(String[] args) {
System.out.println(“HelloWorld”);
}
}

The class HelloWorld has only one method main(). main() is the entry point of a Java program. Method can call methods. For example, in main() method,  the method println() is called.

Method Signature

When you define a method, you have to start with the method signature which is the declaration line for the method:

public static void main(String[] args)

In here what we can learn is:

Who can access the method

public means that the method main () could be accessed by any other Java class or JVM. Java provides some access types to set access modifiers for classes, variables, methods, and constructors. The four access levels are −

  • Visible to the package, the default(No modifiers are needed)
  • Visible to the class only (private)
  • Visible to the world (public)
  • Visible to the package and all subclasses (protected)

Instructions on how to use it

The keyword static means that you don’t have to create an instance (a copy ) of HelloWorld object in memory to use this method. Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.

Does the method return any data

The keyword void means that the method main() doesn’t return any data to the calling program. But if for example, a method had to perform some calculations, it could have returned a resulting number (such as byte, short, int, long, float, double, boolean, and char) to its caller.

Method name

 It is main()

The list of arguments

 The data can be passed to the method, String[] args. In the method main() the String[] args means that an array of Strings can be passed to the method main(). The values that are being passed to a method are called arguments.

Method Body

The body of the method main() has one line

          System.out.println(“HelloWorld”);

Every command or a method call must end with a semicolon ;

Practice

please write your code to print something like:

printhello

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s