Q:

Java program to print message using class

belongs to collection: Java Class and Object Programs

0

Java program to print message using class

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Creating method in same class (public class) in which main method exists:

// Creating method in same class (public class)
// in which main method exists.

import java.util.*;

class HelloWorld {
  public void dispMessage() {
    System.out.println("Hello World.");
  }

  //Main method
  public static void main(String s[]) {
    //creat object of HelloWorld Class
    HelloWorld obj = new HelloWorld();
    obj.dispMessage();
  }
}

Output

Compile: javac HelloWorld.java 
Run: java HelloWorld

Output:
Hello World.

Creating method in other (external) class:

// Creating method in other (external) class

import java.util.*;

//class to print message
class ShowMessage {
  public void dispMessage() {
    System.out.println("Hello World.");
  }
}

//public class
public class HelloWorld {
  //main method
  public static void main(String[] s) {
    //create object of ShowMessage class.
    ShowMessage obj = new ShowMessage();
    obj.dispMessage();
  }
}

Output

Compile: javac HelloWorld.java 
Run: java HelloWorld

Output:
Hello World.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Class and Object Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to find area and perimeter of a circl... >>