What Kind is lt? t Given you an identifier noofStudents in my Java program, do you(most importantly, the computer or the compiler) know what it is? It may be a class name a method name a field name(could be variable or constant a local variable name(data item declared in a method) K凶心 2008-20092a Michael Fung, CS&E, The Chinese University of HK
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 11 What Kind Is It? Given you an identifier noOfStudents in my Java program, do you (most importantly, the computer or the compiler) know what it is? It may be: – a class name – a method name – a field name (could be variable or constant) – a local variable name (data item declared in a method)
What Kind is lt? It all depends on where we put it in our program x Let's now consider the third case. field name When an identifier appears in a data item declaration within a class but not in a method it is a field name class xYz int noofStudentsi JFrame frame double GPA, termGPA K凶心 2008-20092a Michael Fung, CS&E, The Chinese University of HK 12
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 12 What Kind Is It? It all depends on where we put it in our program. Let’s now consider the third case, field name. When an identifier appears in a data item declaration within a class BUT not in a method, it is a field name. – e.g. class XYZ { int noOfStudents; JFrame frame; double GPA, termGPA; }
What Kind is lt? It all depends on where we put it in our program When an identifier appears in a data item declaration within a method. it is a local variable class xYz public static void main(string[] args)( int ofstUdents JFrame frame i double GPA, termGPA K凶心 2008-20092a Michael Fung, CS&E, The Chinese University of HK 13
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 13 What Kind Is It? It all depends on where we put it in our program. When an identifier appears in a data item declaration within a method, it is a local variable. – e.g. class XYZ { public static void main(String[] args) { int noOfStudents; JFrame frame; double GPA, termGPA; } }
Declaration of a Field/ Local Variable *Type refers to both primitive type or class t In general, four basic forms of declarations 1. <type name> identifier; 2. <type name> identifier, identifier 3. <type name> identifier <initial value>; 4. <type name> identifier <initial value1>, identifier <initial value2>,.i t Examples of the last two forms string myName "Michael Fung double Hang SengIndex 17894.03, rainfall =3.4i K凶心 2008-20092a Michael Fung, CS&E, The Chinese University of HK 14
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 14 Declaration of a Field/ Local Variable “Type” refers to both primitive type or class. In general, four basic forms of declarations: 1. <type name> identifier; 2. <type name> identifier1, identifier2, … ; 3. <type name> identifier = <initial value>; 4. <type name> identifier1 = <initial value1>, identifier2 = <initial value2>, …; Examples of the last two forms: String myName = "Michael Fung"; double HangSengIndex = 17894.03, rainfall = 3.4;