Pasting the following in Eclipse produces the error `Cannot make a static reference to the non static method average(double double) from the type Main`
```java
public class Main {
public static void main(String[] args) {
System.out.println(average(4, 5));
}
double average(double a, double b) {
return (a + b) / 2;
}
}
```
Eclipse suggests fixing this by changing the average method to static as follows:
```java
public class Main {
public static void main(String[] args) {
System.out.println(average(4, 5));
}
static double average(double a, double b) {
return (a + b) / 2;
}
}
```
Doing this means that you're no longer thinking in Object Oriented terms. There may be some very good reasons for this.
Here's another way to fix the problem
```java
public class Main {
Main() {
System.out.println(average(4, 5));
}
public static void main(String[] args) {
new Main();
}
double average(double a, double b) {
return (a + b) / 2;
}
}
```
# Example
Take a look at the following two classes. The first one makes everything static, the second doesn't.
1. Can you predict what will happen when you run each class?
2. Can you explain what's going on?
```java
public class Main {
static int count = 0;
public static void main(String[] args) {
Main c1 = new Main();
Main c2 = new Main();
System.out.println(c1.inc());
System.out.println(c1.inc());
System.out.println(c1.inc());
System.out.println(c2.inc());
}
static int inc() {
count++;
return count;
}
}
```
```java
public class Main {
int count = 0;
public static void main(String[] args) {
Main c1 = new Main();
Main c2 = new Main();
System.out.println(c1.inc());
System.out.println(c1.inc());
System.out.println(c1.inc());
System.out.println(c2.inc());
}
int inc() {
count++;
return count;
}
}
```
- [[Solving Section B Questions]]
- [[A Level Section B Questions]]