FreeKB - Java method - Getting Started
Java method - Getting Started

Home > Search


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) {
      e.printStackTrace();
    }
  }
	  
  // make myMethod public accessible
  public static void main(String[] args) {
    MyClass foo = new MyClass();
    foo.myMethod();              
  }
  
}

 

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();
  out.print(foo.myMethod());
%>
 

 

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). 

com.sample.main.MyClass.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 a2a3d in the box below so that we can be sure you are a human.




Comments