Bookmark Topic Watch Topic
Junilu Lacar
Posts: 10411
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
Q: I'm getting a compile-time error that says "cannot make static reference to the non-static method..." How do I fix this?


There are two ways to fix the compiler error related to the message "cannot make static reference to the non-static method..."

1. Invoke the non-static method via a reference to an instance of the class that contains it (more object-oriented style) or

2. Change the non-static method to a static method (more procedural style).


To understand this error message, let's break it down into its different parts.

The "make a static reference" part means that you are trying to use a name (i.e., making a reference to) inside a static context. In other words, the code that is in error is inside a static block of code. Normally, that would be a static method, like the main() method. The example below is typical of the kind of code that produces this error:
Listing 1. Error: illegal static reference to a non-static method

This code will cause a compile-time error with the message "Cannot make a static reference to the non-static method sayHello() from the type Greeter".

You'll notice that unlike the main() method, the sayHello() method is not declared with the static keyword. This makes sayHello() an instance method. That's what the "non-static method sayHello()" part of the compiler error means.

One approach to fixing this problem is to simply add the static keyword to the sayHello() method's declaration as shown below in Listing 2.
Listing 2. A non-object-oriented fix

With this change, the sayHello() method is now a static method so it is perfectly legal to refer to it from the context of the static main() method.

This approach may not be appropriate for all situations though. In fact, we would argue that this type of code does not help you learn how to think in terms of objects and write proper object-oriented programs. We would even go so far as to say that having too many static methods in a Java program is a code smell that indicates there may be too much procedural style code being written.

A more object-oriented approach

The other approach to fixing the problem is more in the object-oriented style of programming. Listing 3 below shows the kind of change you'd need to make to do that.
Listing 3. A more object-oriented fix

Here, the sayHello() method is once again declared as a non-static (instance) method. If we want to keep it this way, we will only be able to refer it from the main() method through an instance of the Greeter class. The "from type Greeter" part of the error message tells you which class you need to instantiate so that you can call the non-static method you're interested in. That's what line 4 does: it instantiates the Greeter class and assigns a reference to the new object to the fred Greeter reference variable. We can now use the fred variable to call the sayHello() instance method on that Greeter object. That's what line 5 is doing.

    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!