• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

What causes non-static method cannot be referenced error?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this piece of code, and what I'm trying to do is scan through a Linked List and check if it's sorted. The method that does this is the following:



However, when I try to call this method from the class that contains this code, which is defined as "public class Node<T extends Comparable<? super T>>", by writing the following code, I get the error "non-static method isSorted() cannot be referenced from a static context."



What does this mean, and how can I solve it? I read that you need to call an instance of the class to fix it, but isn't that what I have done? Thank you for any help.
 
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
remove the static from your testmethod...
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need an object of Node to run that method.
At the moment you are calling Node.isSorted(), and the compiler is looking for a static isSorted method.
 
Marshal
Posts: 80619
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

DD is right. I think that is a misleading error message; it lets people think there is something “normal” about things being static and beginners often change their instance methods to static methods because then the compile‑time error goes away. It is normal for things to be associated with objects, because Java® is an object oriented language. So you call an instance method on an object, and an object is sometimes called an instance of this class. Now an instance method might need access to an instance field, and both the instance method and instance field can be accessed on each object. So, the number of different instance fields is equal to the number of instances. And therefore the number of different ways you can call an instance method is equal to the number of objects.
Now, a static member of a class is different; there is one copy of it. There is one static field per class, irrespective of how many instances there are.
If there is one instance, there is one copy of the static field.
If there are two instances, there is one copy of the static field.
If there are three instances, there is one copy of the static field.
If there are four instances, there is one copy of the static field.
If there are a million instances, there is one copy of the static field.
If there are no instances, there is one copy of the static field.
And the same applies to static methods. So you have one static method and any number of instances. If there are multiple instances, you don't know which object to call the method on. If there are no instances,,, it is impossible to call the method at all. You can only call it if there is one instance, and a static method has no business trying to restrict the class to one instance. So you aren't allowed to derefeerence an instance member from a static context. I think it would be less confusing to say, “Instance method xyz() can only be called from an instance context”. As DD implied, the correct solution usually involves removing the keyword static.
 
Campbell Ritchie
Marshal
Posts: 80619
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That method to test whether the “Node” is sorted looks too complicated for my likings. You have too many dots in line 12. That makes me suspect your design is too complicated. Also, Nodes aren't usually sorted; Lists are. The nodes form parts of the list.
 
reply
    Bookmark Topic Watch Topic
  • New Topic