Syntax of Some Java Components 1. Class Declaration Class ::= ClassModifiers class name ExtendsClause ImplementsClause ClassBody ClassModifier ::= [ public | abstract | final ] public: A class is public. If public is not specified, the class has file scope. abstract: A class that cannot instantiated. final: The class cannot be used as base class. ExtendsClause::= [ extends className ] ImplementsClause ::= [implements interfaceName [{, interfaceName }] 2. Data Member Declaration Data Member :: = DataMemberModifier type dataMemberName initializer [{, dataMemberName initilizer}]; DataMemberModifier :: = [public | private | protected] [static] [transient] [final] initializer ::= [ = expression ] [{, name [ = expression ]] }; public: The member can be accessed anywhere. protected: The member can be accessed in the class and subclasses. private: The member can only be access in the class. none: Accessible within the package. static: Define a class-owned member, not an object-level member. transient: The member will be write to stream. final: Constant variable. It can be initialized when it is declared or before it is accessed. 3. Constructor Declaration Constructor ::= ConstructorModifiers constructorName(ParameterList) ThrowClause ConstructorModifiers ::= [public | protected | private] ParameterList ::= [type name [{, type name }] ThrowClause := [ throws exceptionClass [ {, exceptionClass } ] 4. Member Function (Method) Decalaration Method ::= MethodModifier ReturnType MethodName (ParameterList) ThrowClause MethodBody MethodModifier ::= [public | protected | private] [abstract] [static] [native] [synchronized] [final] abstract: Function without body, and cannot be called. Function must be overridden. final: The function cannot be overridden. static: Define class level function. Class functions are final and are allowed to access static data members only. native: The function is defined in another language. It has not body in class definition. synchronized: Only one thread can call the method at a time. Calls will be blocked until the threads before the caller finish their calls. Besides member function, a block can be synchronized. 5. Local Variable Declaration Local variables are defined in block of or in function. No modifiers such as public, private, static, transient can be used. [final] type { name [ = initializer ] [, name [ = initializer]] }; A local variable can be defined as final (constant). A final variable can be initialized only once at the declaration or later before it is used. 6. Interface Declaration Interface ::= InterfaceModifiers interface InterfaceName extendsClause InterfaceBody InterfaceModifiers ::= [public] [abstract] ExendsClause ::= [ extends interfaceName [{, interfaceName}] ] Public interface can be accessed in other packages. All interfaces are abstract. Data members defined in interfaces are public static and final and methods in interfaces are public and abstract. You don't have to give the modifiers for interface.