/***************************************************************/ /* Prof. Dr. Carsten Vogt */ /* TH Koeln, Fakultaet IME */ /* http://www.nt.th-koeln.de/vogt */ /* */ /* Das Programm demonstriert Unterschiede zwischen lokalen und */ /* globalen Variablen. */ /***************************************************************/ import java.io.*; public class Speiklas { /* globale Zaehlvariable, die von jeder Methode bei ihrem Aufruf um 1 erhoeht wird: */ public static int global = 0; /* Jede Methode besitzt eine lokale Variable local. Eine Methode arbeitet stets auf ihrer eigenen local- Variablen, local-Variablen der anderen Methoden bleiben dabei unveraendert: */ public static void method1() { int local; /* lokale Variable */ global++; local = 110; method2(); System.out.println(); System.out.println("in method1() nach method2-Aufruf: local = " + local); } public static void method2() { int local; /* lokale Variable */ global++; local = 120; System.out.println(); System.out.println("in method2(): local = " + local); } public static void main(String args[]) { int local = 100; /* lokale Variable */ global++; method1(); System.out.println(); System.out.println("in main() nach method1-Aufruf: local = " + local); System.out.println(); System.out.println("nach Abschluss der Aufrufe: global = " + global); } }