0% found this document useful (0 votes)
60 views4 pages

8.11 Class' Static Member

1. Static members can be accessed through a public static method without creating an object of the class. However, static methods cannot access non-static members. 2. Final instance variables must be initialized either during declaration or in every constructor. Initializing constants in constructors allows each object to have different values. 3. Protected members of a superclass can be accessed by subclasses and other classes in the same package. Protected members retain their access level in subclasses.

Uploaded by

Ngọc Chiến
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views4 pages

8.11 Class' Static Member

1. Static members can be accessed through a public static method without creating an object of the class. However, static methods cannot access non-static members. 2. Final instance variables must be initialized either during declaration or in every constructor. Initializing constants in constructors allows each object to have different values. 3. Protected members of a superclass can be accessed by subclasses and other classes in the same package. Protected members retain their access level in subclasses.

Uploaded by

Ngọc Chiến
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

8.

11 Class static member


To access a private static member when no object of the class exist , provide a public static method containing it and call it by qualifying its class following a dot and its name A static member cannot access non-static class member. The this reference cannot be used in a static method. If a static variable isn t initiali!ed , the compiler assign it a default value. A import declaration import only static class member ".g # import staticpac$age%ame.&lass%ame.static'ember%ame(

Final instance variable


)inal variable can be initiali!ed when they re declared. If they are not , they must be initiali!ed in every constructor of class. Initiali!ing constants in constructor of a class enables each object to have a difference values for the constant. To override a superclass method, a subclass must declare a method with the same signature *method name, number of parameters, parameter types and order of parameter types+ as the superclass method

Protected member
A superclass sprotectedmembers can be accessed by members of that superclass, by members of its subclasses and by members of other classes in the same pac$age,protectedmembers also have pac$age access. Allpublicandprotectedsuperclass members retain their original access modifier when they become members of the subclass

8.9 Enumeration (e.g 6.10)


- All enum types are reference types. -"ach enum declaration declares an enum class with the following restriction# .enum constants are implicitly final ,static . attemp to create an object of an enum type with operator new results in a compilation error. -Any constants of enum types must be the first statement in enum s body.

Poloymor !ism
/hen a superclass variable contains areference to a subclass object, and that reference is used to call a method, the subclass version of the method is called

"bstract methods donotprovide implementations. A class that containsanyabstract


methods must be explicitly declaredabstracteven if that class contains some concrete *nonabstract+ methods &onstructors and static methods cannot be declared abstract &onstructors are not inherited, so an abstract constructor could never be implemented &onstructors are not inherited, so an abstract constructor could never be implemented Though non-private static methods are inherited, they cannot be overridden 0.Assigning a superclass reference to a superclass variable is straightforward. 1.Assigning a subclass reference to a subclass variable is straightforward. 2.Assigning a subclass reference to a superclass variable is safe, because the subclass objectis anobject of its superclass. 3owever, the superclass variable can be used to referonlyto superclass members. If this code refers to subclass-only members through the superclass variable, the compiler reports errors. 4.Attempting to assign a superclass reference to a subclass variable is a compilation error. To avoid this error, the superclass reference must be cast to a subclass type explicitly. Atexecution time, if the object to which the reference refers isnota subclass object, an exception will occur. *)or more on exception handling, see &hapter 00.+ 5ou should use the instanceof operator to ensure that such a cast is performed only if the object is a subclass object

6oftware "ngineering 7bservation 00.4

5ou must deal with chec$ed exceptions. This results in more robust code than would be created if you were able to simply ignore the exceptions. &ommon 8rogramming "rror 00.2 A compilation error occurs if a method explicitly attempts to throw a chec$ed exception *or calls another method that throws a chec$ed exception+ and that exception is not listed in that method sthrowsclause. &ommon 8rogramming "rror 00.4 If a subclass method overrides a superclass method, it s an error for the subclass method to list more exceptions in itsthrowsclause than the overridden superclass method does. 3owever, a subclass sthrowsclause can contain a subset of a superclass sthrowslist. 6oftware "ngineering 7bservation 00.9 If your method calls other methods that throw chec$ed exceptions, those exceptions must be caught or declared in your method. If an exception can be handled meaningfully in a method, the method should catch the exception rather than declare it.

#nter$ace
All methods declared in an interface are implicitlypublic abstractmethods, and all fields are implicitlypublic, staticandfinal A class that does not implement all the methods of the interface is an abstractclass and must be declared abstract

%andling e&ce tion


It s a syntax error to place code between atrybloc$ and its correspondingcatchbloc$s. "achcatchbloc$ can have only a single parameter the local variables of a try bloc$ are not accessible in the corresponding catch bloc$s 6oftware "ngineering 7bservation 00.4 5ou must deal with chec$ed exceptions. This results in more robust code than would be created if you were able to simply ignore the exceptions.

&ommon 8rogramming "rror 00.2 A compilation error occurs if a method explicitly attempts to throw a chec$ed exception *or calls another method that throws a chec$ed exception+ and that exception is not listed in that method sthrowsclause. &ommon 8rogramming "rror 00.4 If a subclass method overrides a superclass method, it s an error for the subclass method to list more exceptions in itsthrowsclause than the overridden superclass method does. 3owever, a subclass sthrowsclause can contain a subset of a superclass sthrowslist. 6oftware "ngineering 7bservation 00.9 If your method calls other methods that throw chec$ed exceptions, those exceptions must be caught or declared in your method. If an exception can be handled meaningfully in a method, the method should catch the exception rather than declare it.

You might also like