Java - Understanding methods

Take for example the following markup. In this example, "myMethod" is the method. As we can see, a method exists inside of a bean or class. Also, a method is only invoked when requested. In this example, myMethod is invoked inside of public static void main. Since System.out.println is used, "Hello World" would be appended to the application server's log file.

public class MyClass {

  // create myMethod
  public void myMethod() {
    try {
      System.out.println("Hello World");
    catch (Exception e) {
  // make myMethod public accessible
  public static void main(String[] args) {
    MyClass foo = new MyClass();


Take for example the following markup.

public class MyClass {
  public String myMethod() {
    final String greeting = "Hello World";
    return greeting;


In a JSP page, you could import the bean or class in a JSP page and then call the method. In this example, myMethod is called. This would print "Hello World".

  MyClass foo = new MyClass();


The absolute path to the method being called will be the package (com.sample.main) followed by the bean or class (MyClass / MyBean) followed by the method (myMethod). 



Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter e02f5 in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |