Tuesday, February 4, 2020

Java Research Paper Example | Topics and Well Written Essays - 1750 words

Java - Research Paper Example Members declared public have no bounds, they can be accessed from anywhere by any class irrespective of whether they are in same package or not. To sum up, the access limits of each modifier are tabulated below: Members declared as Can be accessed from members of Same Class Same Package Subclass Other Packages Public Yes Yes Yes Yes Private Yes No No No Protected Yes Yes Yes No b) Span of access Variable that are declared private/protected can be accessed outside its scope by defining set and get methods for that variable in the declaring class and calling the method from outside. However, the set and get methods must be declared public. Alternatively, we can use Reflection API which provides pre-defined methods to access private members of other classes. Still private members of super class cannot be accessed through these methods. c) Example 2. Passing Parameters to methods a) Parameter handling by methods In java, we can pass parameters of any valid data type to methods. This incl udes both primitive data types like integer, string, float etc and reference data types like objects and arrays. In both the cases, the data is passed only by value and not by reference, which means only a copy of the variable is sent and this will not affect the original value of the variable in the internal memory. i. Changing value of a primitive-type parameter within a method: When the value of the primitive type parameter is changed within the method, it remains in effects only within the scope of the function (method). It is not reflected to the outside world and the passed variable still contains its original value, unless until we assign this returned value to the variable. ii. Changing value of a primitive-type data field of a reference-type When the value of object fields of a reference type is changed within the method, it can be reflected in the original object’s field provided it has the proper access level. However, the reference variable will still point to the same object. iii. Reassigning the reference of a reference-type parameter to a new object that you create within a method In this case, the reference variable will point to the new object. b) Example In the above example, only the fields modified inside the method is reflected outside in the reference variable. The actual reference object is not modified outside when it is assigned a new object inside the method. For this reason, the textObject2 is not modified. However, in the next line when the reference variable is assigned a new object returned from the method, it now points to the new object. 3. Static Modifier a) Using the modifier static on a variable A variable is declared static in order to make it accessible commonly among all the instances of a class. In other words, the actual variable can be directly accessed and modified from any objects/instance of a class, just like a common shared folder in a network system. This type of variable is frequently employed in situation where every instances of a class require a common variable to update or retrieve certain information which is common to all users of the application. For example, retrieving or updating the most frequently viewed products in an online shopping application. Further, static variables are employed in combination with public and final keyword to store values that remain constant throughout the application.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.