Jagdish0886
Jagdish0886

Reputation: 501

Groovy:Apparent variable 'classVar' was found in a static scope but doesn't refer to a local variable, static field or class

Getting subject line error while executing simple groovy program

I am new to groovy and java coding, i am getting the subject line error, which I do not think there is a problem with syntax or code

package test.demo

class classExample {

    static void main(args) { 
        // TODO Auto-generated method stub
        classExample classVar = new classExample()
        int result
        result = classVar.sub(5,2)
        println "result is: "+result
    }


    def sub(int var1, int var2){
        return (var1-var2)
    }
}

The function call to sub should be successful without any error

Upvotes: 0

Views: 1545

Answers (1)

injecteer
injecteer

Reputation: 20699

You have 2 major problems with your "class".

  1. name your class by Java names conventions, i.e. starting with capital letter
  2. define arguments or varargs for the sub() method.

The whole thing should look like:

package test.demo

class ClassExample {

    static void main(args) { 
        // TODO Auto-generated method stub
        ClassExample classVar = new ClassExample()
        int result
        result = classVar.sub(5,2)
        println "result is: "+result
    }


    def sub(var1,var2){
        return (var1-var2)
    }
}

Upvotes: 1

Related Questions