Introduction to creating multilpe classes and methods in Java
Methods
In Java it is possible to make an application that, from start to finish, never leaves the main method. But thats not very good design, you'll quickly find yourself copying and pasting a lot of code and it would be nearly impossible to actually fix, read, or even understand your own code. This is why its best to organise your code into different methods and classes and call those methods from within the main method. As you probably know by now, a method is a block of related java statements grouped together in curly braced brackets ("{}") and given a name which can be used to call those java statements to run them.
The following is a simple example of a method which is being called from the main mehtod:
class MethodExample{
public static void main(String args[]){
String message = "Hello World";
print(message);
}
private static void print(String msg){
System.out.println(msg);
}
}
Notice the print() method is called from within the main method and the String message is passed in the bracket. The print message declared below the main method is set up to receive a String in its brackets (note the declaration "print(String msg)"). It then assigns the name "msg" to whatever String it receives. The print method has one line of code, it simply prints msg to the console. The result is that Hello World is printed. This is a simple example passing an argument to a method, you dont have to pass an argument, the brackets can be left empty!
Classes
Think of a class as a file that contains java code. Applications should be spread across many Java classes. This difficult to design and layout your classes the right way, so this is why the process of modeling class of diagrams is so important (A class diagram is similar to an ERD, it is simply a diagram which shows all of your classes, their attributes and the relationships between them).
Most of the test applications we've created to date have contained just one class which had a main method. Now we are going to look at creating a main class and another class and having the two link up.
In this example, lets make a simple application which records and prints student data. We already know that we're going to need a main class to hold our main method. We could just set up a few variables to hold student data in our main class and print them out in the main method. That might seem like a shortcut (and it would be if you only had one student!) but it would make code reuse nearly impossible (imagine declaring the same set of variables again and agian for each student!). The best way forward is to make a student class which will hold all of our student variables, then we can declare many students (and reuse all those variables)!. Let's look at this in practice!
Main class:
public class Main{
public static void main (String args[]){
//Declare our student
//
Student myNewStudent = new Student();
myNewStudent.studentNumber = 108553882;
myNewStudent.studentFirstName = "John";
myNewStudent.studentLastName = "Sun";
myNewStudent.printStudentDetails();
}
}
Student class:
public class Student{
int studentNumber;
String studentFirstName;
String studentLastName;
void printStudentDetails(){
System.out.println("Student number: " + studentNumber);
System.out.println("Student name: " + studentFirstName + " " + studentLastName);
}
}
With the above design. we could create 10 students and reuse the Student class each time.