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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

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

    Code:
    public IBagResult cartesianProduct(IAbstractQueryResult resLeft, 
             IAbstractQueryResult  resRight){
    
             method inside
              }   
             }
    Code:
    IBagResult commaRes = QExecUtils.cartesianProduct(resLeft, resRight);
    qres.push(commaRes);
    non-static method can not be referenced from a static context
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    You must declare your method "cartesianProdu ct" static, if you call it with "QExecUtils.car tesianProduct(. ..)".
    Otherwise, if you want to keep this method non-static, call it by making a new instance of the class:
    Code:
    qExecUtilsInstance= new QExecUtils();
    IBagResult result = qExecUtilsInstance.cartesianProduct(...);

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      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:
      [code=java]public class Example {
      public static void sayHelloStatic( ) {
      System.out.prin tln("Hello!");
      }

      public void sayHelloNonStat ic() {
      System.out.prin tln("Hello!");
      }
      }[/code] Now you can use those methods like so:
      [code=java]//...
      Example.sayHell oStatic();
      //...
      Example ex = new Example();
      ex.sayHelloNonS tatic();
      //...[/code] 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.

      Comment

      • oll3i
        Contributor
        • Mar 2007
        • 679

        #4
        ok ... but i have a static method with non-static variables ?

        Code:
         public static IBagResult ccccc(IAbstractQueryResult resLeft, 
                 IAbstractQueryResult  resRight){
             this.resRight = (List)resRight;
             this.resLeft = (List)resLeft;
         for(int i=0; i<=this.resLeft.size(); i++){
                     
             for(int j=0; j<= this.resRight.size(); j++) {
                 elements.addElements((Integer)this.resLeft.get(i)*(Integer)this.resRight.get(j));
             }   
                 }
         return elements;
        
         }
        says that non-static variables can not be referenced from a satic context

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          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.

          Comment

          • oll3i
            Contributor
            • Mar 2007
            • 679

            #6
            i dont even know how to thank You thank You

            Comment

            Working...