Bootstrap FreeKB - Java - Understanding methods
Java - Understanding methods

Updated:   |  Java articles

Take for example the following markup. In this example, there are three methods, hello and world and main. A method exists inside of a bean or class. Also, a method is only invoked when requested. In this example, the hello and world methods are invoked in the main public static void main method. Since System.out.println is used, "Hello" and "World" would be printed.

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

 


Using the method in another class

Let's say you have two classes, foo.

public class foo {
	public static void hello( ) {
		System.out.println("Hello");
	}
	
	public static String world( ) {
		return "World";
	}
}

 

And bar. Here is how you would use the hello and world methods in the bar class. When using a method defined in some other class, you have to use the structure <class>.<method>  (e.g. foo.hello and foo.world).

public class bar {
	public static void main(String[] args) {
		foo.hello();
		System.out.println(foo.world());
	}
}

 

Let's say the foo class is within the my.pkg package.

package my.pkg;

public class foo {
	public static void hello( ) {
		System.out.println("Hello");
	}
	
	public static String world( ) {
		return "World";
	}
}

 

In this scenario, com.example can be included in the bar class.

public class bar {
	public static void main(String[] args) {
		my.pkg.foo.hello();
		System.out.println(my.pkg.foo.world());
	}
}

 


Using the method in a JSP page

Let's say you have the following class in the my.pkg package.

AVOID TROUBLE

If the class that you want to use does not exist in the same application as your JSP page, you will need to make the class available via one of the following:

  • The JAR file that contains the class can be added to your Java Build Path (Eclipse)
  • The JAR file can be included in the application servers classpath (e.g. JBoss, Tomcat, WebSphere)
  • The JAR file can be included in a Shared Library (WebSphere)
package my.pkg;

public class Hello {
	public static String main() {
		return "Hello World";
	}
}

 

At the top of a JSP page, you would import the class using <package>.<class>.

<%@page import="my.pkg.Hello"%>

 

In the <body> of the JSP page, you could then use the method like this.

  • In this example, Hello is the name of the class (e.g. public class Hello)
  • In this example, example is any custom text you want to use
  • In this example, main is the name of the method (e.g. public static String main)
<%
  Hello example = new Hello();
  out.print(example.main());
%>

 

Here is an example of a JSP page.

<%@page import="my.pkg.Hello"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
  Hello example = new Hello();
  out.print(example.main());
%>
</body>
</html>

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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