Warning: strpos(): needle is not a string or an integer in /home/thebestp/public_html/buildingblocksjava/wp-content/plugins/textlinkads/textlinkads.php on line 175

Warning: strpos(): needle is not a string or an integer in /home/thebestp/public_html/buildingblocksjava/wp-content/plugins/textlinkads/textlinkads.php on line 175

Warning: strpos(): needle is not a string or an integer in /home/thebestp/public_html/buildingblocksjava/wp-content/plugins/textlinkads/textlinkads.php on line 175
Methods | Building Blocks Java

Methods

Data types aren’t much use unless you can do things with them. For this purpose classes have methods. Members say what a class is. Methods say what a class does. For instance our website class might have a method to print its data. If so that would look like this:

class website {

String name;
String url;
String description;

print() {
System.out.println(name + ” at ” + url + ” is ” + description);
}

}

Outside the website method we call the print method just like we referenced the member variables, using the name of the particular object we want to print and the . operator.

website x = new website();

x.name = “Cafe Au Lait”;
x.url = “http://metalab.unc.edu/javafaq/”;
x.description = “Really cool!”;

x.print();

Notice that within the website class we don’t need to use x.name or x.url. name and url are sufficient. That’s because the print method must be called by a specific instance of the website class, and this instance knows what its data is. Or, another way of looking at it, the every object has its own print method.

The print() method is completely enclosed within the website class. All methods in Java must belong to a class. Unlike C++ programs, Java programs cannot have a method hanging around in global space that does everything you forgot to do in your classes.

Constructors
The first method most classes need is a constructor. A constructor creates a new instance of the class. It initializes all the variables and does any work necessary to prepare the class to be used. In the line website x = new website(); website() is a constructor. If no constructor exists Java provides a default one, but it’s better to make sure you have your own. You make a constructor by writing a public method that has the same name as the class. Thus our website constructor is called website(). Here’s a revised website class with a constructor that initializes all the members to null Strings.

class website {

String name;
String url;
String description;

public website() {
name = “”;
url = “”;
description = “”;
}

}

Better yet, we should create a constructor that accepts three Strings as arguments and uses those to initialize the member variables like so:

class website {

String name;
String url;
String description;

public website(String n, String u, String d) {
name = n;
url = u;
description = d;
}

}

We’d use this like so:

website x = new website(”Cafe Au Lait”, “http://metalab.unc.edu/javafaq/”, “Really cool!”);
x.print();

This fits in well with the goal of keeping code relevant to the proper functioning of a class within the class.

However what if sometimes when we want to create a web site we know the URL, name, and description, and sometimes we don’t? Best of all, let’s use both!

class website {

String name;
String url;
String description;

public website(String n, String u, String d) {
name = n;
url = u;
description = d;
}

public website() {
name = “”;
url = “”;
description = “”;
}

}

This is called method overloading or polymorphism. Polymorphism is a feature of object oriented languages that lets one name refer to different methods depending on context. The important context is typically the number and type of arguments to the method. In this case we use the first version of the method if we have three String arguments and the second version if we don’t have any arguments.

If you have one or two or four String arguments to the constructor, or arguments that aren’t Strings, then the compiler generates an error because it doesn’t have a method whose signature matches the requested method call.

toString Methods
Print methods are common in some languages but most Java programs operate differently. You can use System.out.println() to print any object. However for good results your class should have a toString() method that formats the objects data in a sensible way and returns a String. Here’s how we’d implement it in the website example:

public class ClassTest {

public static void main(String args[]) {

website x = new website(”Cafe Au Lait”, “http://metalab.unc.edu/javafaq/”, “Really cool!”);
System.out.println(x);

}

}

class website {

String name;
String url;
String description;

public website(String n, String u, String d) {
name = n;
url = u;
description = d;
}

public website() {
name = “”;
url = “”;
description = “”;
}

public String toString() {
return (name + ” at ” + url + ” is ” + description);
}

}

Comments are closed.