0

I have already programmed with the main method.

unfortunately it doesn't work.

it is programming basic calculator.

public class Taschenrechner {
int zahl1 = 2;
int zahl2 = 6;
int Ergebnis = 0;

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){
    sub(6,2);
    add(6,2);
    mult(6,2);
    div(6,2);
}
}
tonini
  • 13
  • 1
  • 4
  • Hi @tonini, welcome to SO! Please avoid asking questions like "my code doesn't work". Clearly something doesn't work, otherwise why would you be posting here ;) Please include debugging information so others can understand what _exactly_ doesn't work. Please see here for additional information: https://stackoverflow.com/help/how-to-ask – kenny_k Apr 28 '21 at 14:02

1 Answers1

1

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.

Tarmo
  • 3,851
  • 2
  • 24
  • 41
  • **i changed a code. and there was an error "error: non-static method sub(int,int) cannot be referenced from a static context"** ` public static void main(String[] args){ sub(6,2); add(6,2); mult(6,2); div(6,2); } ` – tonini Apr 27 '21 at 18:23
  • Please add your main method code also to the question because error comes from there not from this class. – Tarmo Apr 27 '21 at 18:33
  • But event without the main method code, it's pretty clear whats wrong and you can find your anser from this thread https://stackoverflow.com/questions/4922145/non-static-method-cannot-be-referenced-from-a-static-context-error – Tarmo Apr 27 '21 at 18:36
  • but the other method is not static. that is, it works without the new operator. or did I learn wrong? – tonini Apr 27 '21 at 18:48