As per the Sun Micro Systems , methods are classified into three categories .
1. Mutable Methods
2. Immutable Methods
3. Initialiser Methods
1 . Mutable methods :
- The methods which are used to change the state of the fields ( that is , the values of the fields) , then those methods are known as Mutable methods .
- The Mutable methods not only changes the values of the instance fields , but also changes the state of the local variables .
- Mutable methods are also known as the Setter methods .
- Mutable methods are nothing but a normal methods to a class , they act same as a instance methods to a class , but Mutable method changes the values of the fields .
2. Immutable Methods :
- Immutable Methods are nothing but , they just print the values of the instance fields . That is , immutable methods retrieve the values from the fields .
- In shortly , Immutable methods are the methods which do not cause any change to the fields and values , it just print the values or retrieving the data .
- Immutable methods are also known as Getter methods .
PROGRAM 2 : finding the result of a student
Here the cross mark on the method refers that, when ever the controller comes out of the method , the values which is stored in x , y , z will be deleted with no doubt ,but the values inside the objects will not be deleted . In simple variables are created when their method is called and deleted when their method is left .that is the variable will not hold its value once the controller comes out f the method or scope.
As you can see the above programs existed with mutable method and immutable method . Guess your answer which is mutable and immutable method in each program . IF you are not getting , just comment or ping my blog i always stays on my blog .
Now while coming to scope and life time of a variables , scope is nothing but a block of code which is contained within the curly braces . And the variables are nothing but the local variables declared inside the scope . In Java methods will also acts as scope and the variables declared inside the methods are called local variables .
Now the above explanation will be technically explained in the below topic scope and life time of a variable .
SCOPE AND LIFE TIME OF VARIABLES :
- A block which is beginning with opening curly brace and ended with closing curly brace , then that block is said to be scope . Thus , each time you start a new block , you are creating a new scope .
- A scope determines what objects are visible to other parts of your program . It also determines the lifetime of those objects.
May other computer programming languages define two general categories of scope : global and local .However , these traditional scopes do not fit well with Java’s strict , object-oriented model . In Java , the two major scopes are those defined by a class and those defined by a method . If the method contains the parameters than that parameters are also included in the method scope .
LIFE TIME OF VARIABLES :
Variables are created when their scope is entered, and destroyed when their scope is left . This means that a variable will not hold it’s values once it has gone out of scope .Therefore , variables declared within a method will not hold their values between calls to that method. Also , a variable declared within the block will lose it’s values when the block is left. Thus the life time of the variable is confined to it’s scope .
If a variable declaration includes an initialiser , then that variable will be reinitialised each time the block is in which is declared is entered . For example , consider the following program .
As you can see , ‘y’ is reinitialised to -1 each time the inner for loop is entered .
PLEASE DO VISIT WWW.JAVAFROMBASICS.COM A COMPLETE JAVA BLOG FOR ABSOLUTE BEGINNERS