Keywords: Java Method Invocation | Object Instantiation | Cross-Class Communication
Abstract: This article provides an in-depth exploration of method invocation mechanisms between classes in Java, using a complete file word counting example to detail object instantiation, method call syntax, and distinctions between static and non-static methods. Includes fully refactored code examples and step-by-step implementation guidance for building solid OOP foundations.
Introduction
Method invocation between classes is a fundamental concept in Java object-oriented programming. This article systematically explains how to properly call methods across different classes through a practical file word counting case study.
Problem Analysis
The original code contains several critical issues: First, the WordList words variable is declared but not instantiated, causing NullPointerException when calling the addWord method. Second, directly calling non-static methods from static methods violates Java syntax rules.
Necessity of Object Instantiation
In Java, to call methods from another class, you must first create an instance of that class using the new keyword:
WordList wordList = new WordList();Only with a concrete object instance can you access its member methods through the dot operator.
Static vs Non-Static Methods
Static methods belong to the class level and can be called directly via the class name, while non-static methods belong to the instance level and must be called through object instances. When calling the non-static addWord method from the static method in Words class, you must first instantiate a WordList object.
Complete Code Implementation
Here is the fully refactored code example:
import java.io.*;
public class Words {
public static void main(String[] args) {
WordList result = processInput();
result.printList();
System.out.println("\nProgram execution completed");
}
public static WordList processInput() {
WordList wordList = new WordList();
try (BufferedReader inputFile = new BufferedReader(new FileReader("inputFile.txt"))) {
String inputLine;
while ((inputLine = inputFile.readLine()) != null) {
String[] words = inputLine.toLowerCase().split(" ");
for (String word : words) {
if (!word.trim().isEmpty()) {
wordList.addWord(word);
}
}
}
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
ioe.printStackTrace();
}
return wordList;
}
}
class WordList {
private String[] words;
private int wordCount;
public WordList() {
words = new String[1000];
wordCount = 0;
}
public void addWord(String word) {
if (wordCount < words.length) {
words[wordCount] = word;
wordCount++;
}
}
public void printList() {
for (int i = 0; i < wordCount; i++) {
System.out.println(words[i]);
}
}
}Key Improvements Analysis
1. Instantiate WordList object at the beginning of processInput method
2. Call addWord method using object instance: wordList.addWord(word)
3. Improved addWord method implementation with correct increment operator
4. Added empty string check to avoid counting invalid words
Method Call Syntax Detailed Explanation
The standard syntax for cross-class method calls is: objectInstance.methodName(parameters). The dot operator (.) is used to access object attributes and methods, which is a core feature of object-oriented programming.
Practical Recommendations
For beginners, it is recommended to: understand the relationship between objects and classes, master the instantiation process, and familiarize with method call syntax rules. Build object-oriented programming thinking through practical coding exercises.
Conclusion
Cross-class method invocation is a fundamental skill in Java programming. Proper object instantiation and method call syntax are key to avoiding common errors. The complete examples and detailed analysis provided in this article offer practical learning references for beginners.