Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Using Class and Instance Methods 1. Typically, methods created in classes are instance methods. Instance methods are defined as nonstatic, i. e. without the static modifier. By contrast methods that are used class wide, not to instantiate objects, are class methods. Class methods are generally defined with the static modifier. 2. Since many objects can be instantiated from a class and since each of these objects can have its own settings of the instance variables, it becomes necessary to identify which variable values are to be associated with an object in using, for example, a getInstanceValue method. JAVA uses the this reference to access the correct variable value. 3. Class methods do not have a this reference because no objects are associated with them. 4. Class variables can have access modifiers associated with them. The allowed ones are: public, private, protected, and default. Public allows access to the variable from any class, private allows access only from the class defining the variable, protected allows access within the defining class and by classes inherited from this class. Default modifier allows access of class variables to any class within the same package of classes. 5. Access modifiers for variables can only be used on class variables. Variables defined in methods cannot have access modifiers. 6. Class variables that affect every object instantiated from the class can be offered as shared variables by enhancing their access to be static. Thus Class Employee { static private String FIRM_NAME = “Widgets Inc.”; } will make available the firm name, Widgets Inc., to every instance of the Employee class. All references to FIRM_NAME will reflect a change to its value. Thus if Widgets Inc. is changed in the definition of FIRM_NAME to Toys Inc. all Employee class instances will automatically get the new name. 7. The final keyword can be used in the definition of a variable to make its value constant. Such constant variables must be defined with an initial value. Once defined the value cannot be changed dynamically. For example, final int maxStudents = 45; can be used to permanently set the maximum number of students who may be admitted to a course. 8. A JAVA package is a collection of related classes. So far we have not had to import any specific classes in our work with J++. The Java.Lang package is automatically imported into JAVA programs. If, however, we need to use specific sub classes within Java.Lang we must specify their use explicitly in our statements. One of the most useful classes is the Math class of the Java.Lang package. A detailed list of useful Math methods are to be found on page 188 of your text. We will most likely use Abs(x), Ceil(x), Floor(x), Pow(x,y), Random(), and Round(x). Abs(x) -- Absolute value of x Ceil(x) – Smallest integral value not less than x Floor(x) -- Largest integral value not greater than x Pow(x,y) – x raised to power y Random(x) – Random double number between 0.0 and 1.0 Round(x) – Closest integer to x where x is float or double and the return value is int or long. Examples: p = Math.Abs(-2.3); // Places 2.3 in p as double q = Math.Ceil(2.1); // Places 3 in q as integer r = Math.Floor(2.1); // Places 2 in r as integer s = Math.Pow(q,r); //Places 9.0 in s using the previously received values of q and r, s as double. t = Math.Round(2.4); // Places 2 in t, defined as long. 9. The object browser can be used to get more information on the content of objects and method in the packages associated with the JAVA language.