Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.

Similar presentations


Presentation on theme: " Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor."— Presentation transcript:

1  Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor Overloading

2 The static modifier associates a variable or method with its class rather than with an object of the class. Non – Static FieldsStatic Fields Also called as instance variablesAlso called as class variables Each object of the class will have its own copy of each of the instance variables that appear in the class definition. A class will have only one copy of each of its static fields, and these will be shared among all the objects of the class. Each object will have its own values for each instance variables. Each class variable exists even if no objects of the class have been created. If the value of a static field is changed, the new value is available equally in all the objects of the class. A static field or method is defined using the keyword static preceding the type name.

3 In the following code, the class “T2” has two variables x and y. The y is declared with the keyword “static”. In the main class “T1”, we try to manipulate the variables x and y in “T2”, showing the differences of instance variable and class variable.

4

5 Note that the static methods are called using class Names.

6

7 Argument Promotion:  Java will promote a method call argument to match its corresponding method parameter according to the promotion rules.  Values in an expression are promoted to the “highest” type in the expression (a temporary copy of the value is made).  Converting values to lower types results in a compilation error, unless the programmer explicitly forces the conversion to occur.  Place the desired data type in parentheses before the value Example: ( int ) 4.5

8

9 Basic scope rules:  Scope of a parameter declaration is the body of the method in which appears  Scope of a local-variable declaration is from the point of declaration to the end of that block  Scope of a local-variable declaration in the initialization section of a for header is the rest of the for header and the body of the for statement  Scope of a method or field of a class is the entire body of the class

10 Shadows field x Display value of local variable x

11 Shadows field x Display value of local variable x Display value of field x

12

13 In JAVA, it is possible to create methods that have the same name, but different parameter lists and different definitions. This is called method Overloading. When we call a method in an object, java matches up the method name first and then number and type of parameters to decide which one of the definitions to execute. This process is called polymorphism. Differences in return type are irrelevant in method overloading  Overloaded methods can have different return types  Methods with different return types but the same signature cause a compilation error

14 Correctly calls the “ square of int ” method Correctly calls the “ square of double ” method Declaring the “ square of int ” method Declaring the “ square of double ” method

15

16 Like methods, constructors can also be overloaded. Since the constructors in a class all have the same name as the class, />their signatures are differentiated by their parameter lists. Example: A cube class containing 2 constructors. Default and Parameterized

17

18 It is possible to use this() construct, to implement local chaining of constructors in a class. The this() call in a constructor invokes the an other constructor with the corresponding parameter list within the same class. Calling the default constructor to create a Cube object results in the second and third parameterized constructors being called as well. Java requires that any this() call must occur as the first statement in a constructor. Example: A cube class containing 3 constructors which demonstrates the this() method in Constructors

19

20


Download ppt " Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor."

Similar presentations


Ads by Google