Anyone just starting out with Java development is likely to encounter the "Cannot Make a Static Reference to a Non-Static Method" error. This is a common issue particularly in cases where a Java project contains method declarations within its main class, or is calling methods which are not static, from static methods. Understanding the causes and solutions to the problem is a key element in learning how to construct Java projects.
If a Java program attempts to call a method that is not static, from a method that is static, the compiler or IDE (Integrated Development Environment) will generate an error. Programmers in the early stages of learning Java development are most likely to encounter this error, because they may not yet be familiar with the concepts involved in Object Oriented development through the Java language. A common situation in which the error tends to arise is when a program's main method is attempting to call another method defined in the same class, but which has not been declared as a static method.
The solution to problems calling non-static methods from static methods really needs to be approached in a way that suits the application. Making the method static by adding the "static" keyword to the method outline may stop the compiler from complaining and allow a program to run. However, the occurrence of the error may indicate that the method could be better handled by including it in a class declaration, calling it by first creating an object of the class.
When Java developers design applications, they divide up the various processing tasks required between a set of objects. These objects each exhibit a well-defined set of properties and behaviors, as outlined in their class declarations. Developers can therefore design object types to carry out application tasks, formalizing these designs by writing class declarations, within which variables and methods are outlined.
Once an application project has one or more class declarations included in it, the code within the application can create object instances. An object instance is a dedicated code element with the variables and methods outlined in its corresponding class declaration. For example, given the presence of a class declaration called "Helper" within a Java project, code within the project, for example inside the main method, can create an object instance of the class using the following sample syntax:
Helper myHelper = new Helper();
If, within the "Helper" class declaration, there is a public method called "doSomething," the code can call that method on the object instance as follows:
The most common situation in which the static reference error occurs is when a program attempts to call methods from the main method of a project. The main method is normally static, so code within it cannot call static methods. If a programmer creates a method declaration within the same class as the main method, the error will arise unless the method is defined as static. Choosing to create objects which carry out well-defined sets of application activities allows programmers to more effectively divide up the elements within an application as a whole.
- Photo Credit Photodisc/Photodisc/Getty Images