By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,563 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,563 IT Pros & Developers. It's quick & easy.

non-static method can not be referenced from a static context

oll3i
100+
P: 679
Expand|Select|Wrap|Line Numbers
  1. public IBagResult cartesianProduct(IAbstractQueryResult resLeft, 
  2.          IAbstractQueryResult  resRight){
  3.  
  4.          method inside
  5.           }   
  6.          }
Expand|Select|Wrap|Line Numbers
  1. IBagResult commaRes = QExecUtils.cartesianProduct(resLeft, resRight);
  2. qres.push(commaRes);
non-static method can not be referenced from a static context
Oct 28 '13 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 785
You must declare your method "cartesianProduct" static, if you call it with "QExecUtils.cartesianProduct(...)".
Otherwise, if you want to keep this method non-static, call it by making a new instance of the class:
Expand|Select|Wrap|Line Numbers
  1. qExecUtilsInstance= new QExecUtils();
  2. IBagResult result = qExecUtilsInstance.cartesianProduct(...);
Oct 29 '13 #2

Nepomuk
Expert 2.5K+
P: 3,112
Your error message can be explained as follows:

In Java, functions, fields and inner classes can be static if they use the modifier static. These functions / fields / classes do then not belong to an instance of the wrapping class but rather to the outer class itself. Let me give you an example:
Expand|Select|Wrap|Line Numbers
  1. public class Example {
  2.     public static void sayHelloStatic() {
  3.         System.out.println("Hello!");
  4.     }
  5.  
  6.     public void sayHelloNonStatic() {
  7.         System.out.println("Hello!");
  8.     }
  9. }
Now you can use those methods like so:
Expand|Select|Wrap|Line Numbers
  1. //...
  2. Example.sayHelloStatic();
  3. //...
  4. Example ex = new Example();
  5. ex.sayHelloNonStatic();
  6. //...
You see, for the second one you need an instance of that class while you don't for the first.

The error you're getting occurs when you are trying to call a non-static function from a static one without creating an instance of that object to use it from. This quite often happens in the main function (which is static). So the solution would be
  • to create an instance of the object and use the function from there or
  • make the function static
In many cases the first solution is to be preferred but there can be very good reasons to go with the second one too.
Oct 29 '13 #3

oll3i
100+
P: 679
ok ... but i have a static method with non-static variables ?

Expand|Select|Wrap|Line Numbers
  1.  public static IBagResult ccccc(IAbstractQueryResult resLeft, 
  2.          IAbstractQueryResult  resRight){
  3.      this.resRight = (List)resRight;
  4.      this.resLeft = (List)resLeft;
  5.  for(int i=0; i<=this.resLeft.size(); i++){
  6.  
  7.      for(int j=0; j<= this.resRight.size(); j++) {
  8.          elements.addElements((Integer)this.resLeft.get(i)*(Integer)this.resRight.get(j));
  9.      }   
  10.          }
  11.  return elements;
  12.  
  13.  }
says that non-static variables can not be referenced from a satic context
Oct 29 '13 #4

Nepomuk
Expert 2.5K+
P: 3,112
Correct. The this operator is trying to access an instance of the class. As you're using a static function there is no instance to access.

In this case I'd think you don't want your function to be static.
Oct 29 '13 #5

oll3i
100+
P: 679
i dont even know how to thank You thank You
Oct 29 '13 #6

Post your reply

Sign in to post your reply or Sign up for a free account.