Eine gute Antwort wäre:

  private void inkrementZaehler()
  {
    zaehler = zaehler + 1;
  }

main() kann keine private Methode verwenden

Hier ist eine main() Methode, die irrtümlicherweise versucht die Variable zaehler zu ändern:

class Bankkonto
{
  private String kontonummer;
  private String kontoinhaber;
  private int    kontostand;
  private int    zaehler = 0;
  . . . .

  private void inkrementZaehler()
  . . . .
}

class BankkontoTester
{
  public static void main( String[] args )
  {
    Bankkonto bobsKonto = new Bankkonto( "999", "Bob", 100 );

    bobsKonto.verarbeiteAuszahlung( 50 );
    bobsKonto.inkrementZaehler();
    bobsKonto.zaehler = 15;
  }
}

FRAGE 9:

Wird dieses Programm kompilieren und sich ausführen lassen?

Inhaltsverzeichnis