(ObjectOriented) HelloWorld

71 1 0
                                        

public class HelloWorld{

         public static void main(String[] args){

                     AnotherClass ac = new AnotherClass();

                      ac.display();

         }

}

class AnotherClass{

       public void display(){

              System.out.println("Hello World");

      }

}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

How do you read the program?

You look for the word main. Main method is the driver of your program. So to read this, an object "ac" was created from class "AnotherClass" which is an outside class from your main class which is "HelloWorld". And using the object "ac", we call the method named "display" whose functions from "AnotherClass" was to print the word "Hello World". So, if you run this program, your output should be:

 Output: Hello World

Below was the definition of the words and symbols inside the program. I just made it up based on how i understand it but i did not emphasize the definition that much.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/* MEANINGS OF EACH WORD OR SYMBOL INSIDE THE PROGRAM */

public class HelloWorld{} - this is the format of the class.

public - modifier of the class . A modifier is a way of knowing on how accessible those class are.

class - you can write your code inside a class.

HelloWorld - is the name given to that class.

{} - inside is where the body of the program lies.

public static void main(String[] args) - a method with an array parameter.

public - a modifier of the method.

static - another modifier of the method.

void - no return type.

main - name of the method.

(String[] args) - this is a parameter or an arguments

String - is a data type.

[] - symbolize that it is an array.

String[] - an array of String.

args - the variable name of the parameter.

AnotherClass ac = new AnotherClass(); - format on creating an object.

AnotherClass - this is a class Name.. like helloWorld.

ac - this is the name of the object.

new - is the keyword use to create an object.

AnotherClass() - this is a no argument constructor.

; - end of the statement;

ac.display(); - calling the method "display" through an object "ac" of the outside class.

. - is a connection

display() - method named "display" without parameters

() - symbolize no parameters.

class AnotherClass{} - an outside class of your main class

public void display(){} - this is the method display

display - name of the method.

System.out.println("Hello World"); - this is how print on a command prompt.

System - is a class inside the built in package which is a "java.lang"

out - is an inner field of the class System.

println - means print it out and then put a new line after so that the next print would be on a new line.

"Hello World" - this is the data of the type string that we print on the command prompt



Java codes i learned onlineWhere stories live. Discover now