Java - Understanding class

class is a collection of markup that performs a certain function. To use an analogy, the military has a class of officers classifed as privates, and a different class of officers classified as admirals. When combat is needed, the officers from the privates class will be used. When negotiation is needed, the officers from the admirals class will be used. In the same way, when a Java programs needs to use certain objects, the class that contains the objects will be used.

Let's say you've created a class called Hello.java (in Eclipse in this example). 

 

In this example, the Hello.java class is in the my.pkg package. There is a public static void method called main that will print Hello World.

package my.pkg;

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

 

In this example, there are three methodshello and world and main. The hello and world methods are invoked in the 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());
	}
}

 

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, my.pkg 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 class 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

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 85cc2 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |