Java program needs a starting point which is the main method. You can't run anything without a main method. But you can compile without main method. You can compile loads of classes and other constructs, package it as a jar and add it as a dependency to some other code which has a main method.
Edit (as the question was changed to concrete compilation error)
You are trying to use an instance method (method of a class without the static
keyword) from static context (from a method which has the keyword static
/ main method). In order to use an instance method, one needs an instance of a class created with new
keyword. Your example would go like this:
public class Taschenrechner {
// Removed instance variables from here because they were not actually used.
// You should only use instance variables when you want to store state between your method calls (store the last calculation to undo it for example)
public int sub(int zahl1, int zahl2 ){
int Ergebnis = zahl2 - zahl1;
return Ergebnis;
}
public int add(int zahl1, int zahl2){
int Ergebnis = zahl2 + zahl1;
return Ergebnis;
}
public int mult(int zahl1, int zahl2){
int Ergebnis = zahl2 * zahl1;
return Ergebnis;
}
public double div(int zahl1, int zahl2){
double Ergebnis = (double) zahl1/zahl2;
return Ergebnis;
}
public static void main(String[] args){
Taschenrechner taschenrechner = new Taschenrechner();
int subtractionResult = taschenrechner.sub(6,2);
int addingResult = taschenrechner.add(6,2);
int multiplicationResult = taschenrechner.mult(6,2);
int divisionResult = taschenrechner.div(6,2);
System.out.println("subtractionResult = " + subtractionResult); // prints 4
System.out.println("addingResult = " + addingResult); // prints 8
System.out.println("multiplicationResult = " + multiplicationResult); // prints 12
System.out.println("divisionResult = " + divisionResult); // prints 3
}
}
Also note that I removed the instance variables from your code because they were unused as they were shadowed by method scope variables.