Demystifying Hello World in Java !

Majority of the folks in the programming community start with the plain old “Hello World” when experimenting with a new language.

Let’s print “Hello World” in PHP.

<?php 
echo "Hello World";
?>

Too lazy, use short tags (not recommended):

<?="Hello World"?>

In JavaScript :

console.log("Hello World");

Let’s enter the Java World.

public class DemoClass{  public static void main(String[] args){
System.out.println("Hello World");
}
}

Everything in Java has to be enclosed within a class. A class can be any blueprint that describes the states and behaviours of objects. An object is an instance of a class. An object has state and behaviour.

Blueprint of an iPhone 5

This is a simple blueprint of an iPhone 5. It includes a home button, volume rockers and a mute switch. There is power button on the top and a few sensors on the top portion of the display. The front-facing camera also lies on the top. It has a Multi‑Touch display and many more features.

The main purpose of a blueprint is to describe the features and various components of anything we wish to develop; inherently abstracting the complexity involved. We can create many instances of a class having a specific state and behaviour. In the above case, we can manufacture multiple iPhone 5’s with this blueprint; some maybe defective (a faulty camera, button, etc) implying not all objects will have the same state. Components of an object should not be accessed directly. They should be accessed with the help of methods. Methods are functions that exist inside a class.

Switching to our Hello World Example

public simply implies that the class is visible/accessible to everyone. DemoClass is the name of the class I created.

//public implies the class is accessible to everyone.
public class DemoClass{

main() is the starting point of an application in Java. Whenever we run a Java program, main() is the first method to be called.

//We want our main method to be public since an external agent(outside the class) will call the main method.
//void simply implies the main method does not return any value.
public static void main(String[] args){

What is the “static” keyword? Any method or field with the keyword static, by convention, has to be called by the Classname. So some external agent will make a call to main() method not by creating an instance of the enclosing class rather just by Classname.main(args …); e.g, DemoClass.main().

NOTE: dot(.) operator is used to access/call the components of an object.

//static implies method will be called with the help of Classname
//args
consists of an array of data passed by an external agent
public static void main(String[] args){

Any method or field without the static keyword is a non-static method or field. Non-static members need to be called with the instance of the class.

println() is a non-static method inside the PrintStream class. Non-static method needs to be called with the instance of the class. The out field inside System class is an instance of the PrintStream class.

To call println() on PrintStream instance, we make use of the dot(.) operator.

//println() is a non-static method in PrintStream class
//We make use of the out instance to call println()
out.println("Hello World");

The out field inside System class is an instance of the PrintStream class but it is a static field. As we all know, static fields need to be called with the enclosing classname.

Hence, we have: System.out.println()

//public implies the class is accessible to everyone
public class DemoClass{
//public implies the main method is accessible to everyone
//static implies the method can be called with the classname
//void indicates the method does not return any value
public static void main(String[] args){

/
/println is a non-static method inside PrintStream class
//out is an instance of PrintStream class
//out is a static field inside System class
System.out.println("Hello World");
}
}

To summarize,

  • A class is a blueprint of any object describing it’s state and behaviour.
  • An object is an instance of a class.
  • Functions existing inside a class are called as methods.
  • Variables existing inside a class are called as fields.
  • Static members need to be called with classname.
  • Non-static members need to be called with the instance of the class.

Developer @Shopify

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store