473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

oll3i
679 512MB
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
5 6463
chaarmann
785 Expert 512MB
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
3,112 Expert 2GB
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
679 512MB
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
3,112 Expert 2GB
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
679 512MB
i dont even know how to thank You thank You
Oct 29 '13 #6

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

Similar topics

3
by: John Bowling | last post by:
I'm trying to get the day of month with: int dayofmonth; dayofmonth = Calendar.get(Calendar.DAY_OF_MONTH); and I get the compile time error of saying get(int) is a not-static method. Is...
1
by: Taylor | last post by:
"Static" is synonymous with "class" with regards to members (methods and fields) right? Sometimes I hear refrence to the "static method" and sometimes its refered to a "static method." Is there...
7
by: Chris Clement | last post by:
I have been handed a project that someone else started and most of it was developed in the VS.NET design mode. For whatever reasons, when I try to make changes to the controls in VS.NET design...
4
by: TS | last post by:
I am trying to create a page template. I am trying to set the value of a field to the return value from a method. What do I have to do to get m_form set to the value returned from a method? (the...
3
by: Lucy Ludmiller | last post by:
I have code like this : void foo(int i, void* p) { .. } class A { public: A(); ~A();
4
by: Chris Brat | last post by:
Hi, I've done some reading up but I cant get my head around how/why class methods can be used as apposed to static methods. Can anyone give an example of where they have used class methods? ...
5
by: Logan Lee | last post by:
This is a method I have devised. Not really organized but http://www.geocities.com/logan.lee30/Definitions_and_examples.pdf. Definitions I=if; EI=else if; E=else (I) is one of I, EI, E...
5
by: uhdam | last post by:
Hi I am unable to access a non static variable in a static context The variable is an arrayList and in a separate file I cannot change the corresponding non-static class into static...
8
by: blknmld69 | last post by:
I am working on a major project. I keep getting the error non-static variable calcGrade cannot be referenced from a static context calcGrade.studentList = new Student(); import java.util.*; ...
1
by: mcfly1204 | last post by:
I have two classes in the same namespace, Form1 and NewAccount. I am attempting to call an instance method that will reload a combobox on Form1. I have Form1 implemented as a singleton, and a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.