Lecture 4
Lecture 4
references
Lecture 4
1 / 16
Contents
1 Immutable objects
2 Immutable class
3 Scope of Variable
2 / 16
Immutable objects
Immutable simply means unmodifiable or unchangeable. An immutable value is one which
you are not allowed to change.
Immutability is a rule of the programming language where The compiler makes sure that
your program doesn’t try to change the any immutable values and refuses to compile it if it
does.
In Java this can be done by means of keyword ’final’ as follows:
Program 4.1:
p u b l i c c l a s s Alpha
{
f i n a l s t a t i c i n t x =10; / / x i s immutable
s t a t i c i n t p =5; / / p i s m u t t a b l e
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] args )
{
System . o u t . p r i n t l n (++ Alpha . p ) ; / / ok , no e r r o r
System . o u t . p r i n t l n ( Alpha . x ) ; / / ok , no e r r o r , we can o n l y use i t
System . o u t . p r i n t l n (++ Alpha . x ) ; / / e r r o r , we can n o t modify i t
}
}
Output:
Alpha.java:8: error: cannot assign a value to final variable x System.out.println(++Alpha.x);
We know that objects are variables of class which store values for instance variables. The
values of instance variable of an object may be changed later even after once the object
has been created. There may be some situation arrives, when it is desirable to create an
object whose contents cannot be changed once the object has been created. This type of
object is termed as immutable object and its class as immutable class. 3 / 16
Immutable class
Immutable class means that once an object is created, we cannot change its content.
In Java, all the wrapper classes (like Integer, Boolean, Byte, Short) and String class is
immutable. We can create our own immutable class as well.
NB:An immutable object is an object whose internal state remains constant after it
has been entirely created.
For example in Java, String class is immutable and so string objects are immutable objects.
Let us consider the following program.
Program 4.2:
p u b l i c c l a s s Alpha{
p u b l i c s t a t i c v o i d main ( S t r i n g args [ ] ) {
S t r i n g s= ” Sachin ” ;
s . concat ( ” Tendulkar ” ) ; / / concat ( ) method appends t h e s t r i n g a t t h e end
System . o u t . p r i n t l n ( s ) ; / / w i l l p r i n t Sachin because S t r i n g s are immutable o b j e c t s
}
}
Output:
Sachin
In the above program, We have created a String reference ’S’ that refers to a memory
location in string pool with value ’Sachin’.
We have called a string append function concat() which is used to concatenate ’Tendulkar’
at the end of object value ’Sachin’. As a result a separate memory is created that contains
value ’Sachin Tendulkar’.
As String objects are immutable, the reference ’S’ always refers to the same memory,
valued with ’Sachin’. Hance the the out put for the program is ’Sachin’ rather than ’Sachin
Tendulkar’.
5 / 16
Continue...
f i n a l c l a s s Student
{
p r i v a t e S t r i n g name ;
p r i v a t e f i n a l i n t regdNo ;
f i n a l s t a t i c S t r i n g branch= ”CSE” ;
Student ( S t r i n g S1 , i n t i d )
{
name=S1 ;
regdNo= i d ;
}
void display ( )
{
System . o u t . p r i n t l n ( name ) ;
System . o u t . p r i n t l n ( regdNo ) ; \ \ ok , we can use i t
\\System . o u t . p r i n t l n (++ regdNo ) ; \ \ e r r o r , As f i n a l , so no
\\ m o d i f i c a t i o n i s a l l o w e d
}
}
p u b l i c c l a s s Test
{
p u b l i c s t a t i c v o i d main ( S t r i n g args [ ] )
{
Student s = new Student ( ” Sachin ” , 123456);
s . display ( ) ;
System . o u t . p r i n t l n ( Student . branch ) ;
\\s . regdNo = 1028; \\show e r r o r , regdNo i s p r i v a t e
\\s . branch= ” CSIT ” ; \\ show e r r o r , branch i s f i n a l
}
}
6 / 16
Continue...
Output:
Sachin
123456
CSE
As shown in the above example the class Student declare as final with all the data fields
either private, private final or final.
All the data fields are initialized by a parameterised constructor without any setter
functions. The data fields name and regdno are private in nature, so they can only access
inside the class and statement like ’s.regNo = 1028;’ outside the class will generates error.
Another data field ’branch’ is declared as final static but not private. In this case we can
use ’branch’ data field outside the class using the statement ’Student.branch’ but we can
not modify by statement like ’s.branch=”CSIT”;’ that generates error.
The data field ’regdNo’ is declared private as well as final, so neither we can modify it
inside the class nor access out side the class.
7 / 16
Scope of Variable
All most all the programs need some inputs/values to operate on it.
A variable is a container which stores these inputs/values while the Java program is
executed.
Like other programming languages, scope of a variable is the part of Java that refers to the
visibility of variables in a program. In other words, which parts of your program can see or
use a variable.
In lecturer note 25, we have already discuss about the three types of variables in java such
as local variables, instance variables and class/static variables.
Let us discuss these variables based on their scope inside the program.
Scope of Local variables: Local variables are declared inside a method,
constructor, or block. The scope of these variables remains active within the method,
block, or constructor. As you exit from the method or block then the scope of a local
variables are destroyed.
Program 4.4:
NB: Program is in next Slide.
Output:
ITER is in Campus 1
In the preceding example program, we cannot access the local variable “x” from
outside the method display(). If we try to access it, we will get compile-time error.
8 / 16
Continue...
p u b l i c class SOA University
{
S t r i n g collegeName ;
S O A U n i v e r s i t y ( S t r i n g S1 )
{
collegeName=S1 ;
}
void display ( )
{
i n t x =1;\\ L o c a l v a r i a b l e
System . o u t . p r i n t l n ( collegeName+ ” i s i n Campus ” +x ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] args )
{
S O A U n i v e r s i t y S=new S O A U n i v e r s i t y ( ” ITER ” ) ;
S. display ( ) ;
\\System . o u t . p r i n t l n ( ” Campus i s : ” +x ) ; \ \ show e r r o r
}}
Output:
38760.0
41820.0 10 / 16
Continue...
Scope of class/static variables: These variables are visible inside the class, but scope of
these variables are through out the program.
We know that static variables are not part of object. So, for static variable memory is
created only once in class area and all the objects can able to access it any time any
where inside a single file.
All the methods, constructors inside the class can directly access it. But outside the class
and inside the same file, it can be access by classname.staticvariableName.
Program 4.6:
NB: Program is in next Slide.
Output:
Item Name: Dove Quantity: 5
Item Name: Liril Quantity: 6
Item Name: Lux Quantity: 3
Total object created (total items are): 3
11 / 16
Continue...
c l a s s Item {
p r i v a t e S t r i n g itemName ;
private int quantity ;
s t a t i c i n t count = 0 ; / / v a r i a b l e t o count
Item ( S t r i n g S1 , i n t p )
{
itemName = S1 ;
quantity = p;
count ++;
}
p u b l i c v o i d showItem ( )
{
System . o u t . p r i n t l n ( ” Item Name : ” + itemName +
”\tQuantity : ” + quantity ) ;
}
}
p u b l i c c l a s s Alpha
{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] args )
{
Item I 1 = new Item ( ” Dove ” , 5 ) ;
Item I 2 = new Item ( ” L i r i l ” , 6 ) ;
Item I 3 = new Item ( ” Lux ” , 3 ) ;
I 1 . showItem ( ) ;
I 2 . showItem ( ) ;
I 3 . showItem ( ) ;
System . o u t . p r i n t l n ( ” T o t a l o b j e c t c r e a t e d ( t o t a l i t e m s are ) : ”
+ Item . count ) ;
}
}
12 / 16
this: A Reference Variable
this is a keyword in Java acts as a reference variable. Following are the ways to use ‘this’
keyword in Java.
this refers to the current object. The object which is being processed upon by JVM is
termed as current object that means object that invokes the member function of a
class.
Program 4.7:
p u b l i c c l a s s Alpha
{ void input ( )
{
System . o u t . p r i n t l n ( ” t h i s r e f e r e n c e = ” + t h i s ) ; / / t h i s r e f e r s t o A1
}
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] args )
{
Alpha A1= new Alpha ( ) ;
A1 . i n p u t ( ) ; / / A1 i s t h e c u r r e n t o b j e c t
System . o u t . p r i n t l n ( ” o b j e c t r e f e r e n c e = ” + A1 ) ;
} }
Output:
this reference = Alpha@6d06d69c
object reference = Alpha@6d06d69c
In the above example, we created an object named A1 of the class Alpha. We then
print the reference to the object A1 and this keyword of the class. Here, we can see
that the reference of both A1 and this is the same. It means ’this’ is nothing but the
reference to the current object. 13 / 16
Continue
this is used to differentiate between the instance variables and local variables.In Java,
it is possible to declare both instance and local variables name same.
Now it is mandatory to use this keyword because the Java compiler gets confused due to
name ambiguity between instance and local variable.
14 / 16
Continue
this can be used to invoke a function from within another function inside the class they
belong to as follows:
Program 4.8:
p u b l i c c l a s s Alpha {
void input ( )
{
System . o u t . p r i n t l n ( ” Welcome t o ITER ” ) ;
}
void display ( )
{
t h i s . i n p u t ( ) ; / / F i r s t s t a t e m e n t and i n v o k e i n p u t ( ) f u n c t i o n
System . o u t . p r i n t l n ( ” Welcome t o SOA ” ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] args )
{
Alpha A1= new Alpha ( ) ;
A1 . d i s p l a y ( ) ;
} }
Output:
Welcome to ITER
Welcome to SOA
Here we have not called the input() method inside the main() method, still A1 can access it
indirectly through display() using this as shown in the program.
NB:If you want to invoke a member function inside another member function using
this, then the function invocation statement (ex: this.input();) must be the first
statement inside the body part of the function.
15 / 16
References
16 / 16