1

Here is the my class..

public class Oop {
    int count = 0;

    public static void main(String args[])
    {
        this.count(15, 30);
        System.out.print(this.count);
    }

    public void count(int start, int end)
    {
       for(;start<end; start++)
       {
           this.count = this.count + start;
       }
    }
}

I can't call count function inside of main function. Reason is static and non-static functions. I'm really new for Java. How can i use count inside of main? What i need to learn?

Yusuf
  • 219
  • 1
  • 2
  • 7
  • 3
    Now, *search* for the error message [removing the non-generic names] on SO as there are many duplicates. You *must* create an instance to invoke a non-static method upon it. – user2246674 Jul 23 '13 at 23:48

3 Answers3

8

You need to instantiate Oop and then call the method with it, like this:

Oop oop = new Oop();
oop.count(1,1); 

For further information check this out: Difference between Static methods and Instance methods

Community
  • 1
  • 1
jsedano
  • 4,088
  • 2
  • 20
  • 31
2

You should make the count function and the count variable static too.

tbodt
  • 16,609
  • 6
  • 58
  • 83
1

That's the least of your worries - once you've called the method, you won't be able to access the result.

You must create an instance of your class, and use that instance to call your method and get the result:

public static void main(String args[]) {
    Oop oop = new Oop();
    oop.count(15, 30);
    System.out.print(oop.count);
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722