Chap2 Part2
Chap2 Part2
Programming
Mrs. Apurwa Barve
static keyword (IMCC)
class
static
method variables
keyword
imports
Any member can de made static by using the keyword, ‘static’ in its declaration.
Mrs. Apurwa Barve
static keyword - variables (IMCC)
By rule all the instance variables are copied in every object. Hence as many objects of a
class we create, those many copies of instance variables are created. Each copy will
hold its own values within it. Let’s say we have a class Square with instance variables,
side, area, volume. As many objects I create of Square class, all those objects will have
their OWN values of side, area and volume because instance variables will be copied
separately for them.
When you need to share common values between different objects of your class you
make that value STATIC.
static is a keyword in Java which is a non-access modifier. Syntax is:
accessmodifier static variableName[=initialization value];
When a variable is declared static, its copy is NOT made for every variable. Only one
copy is created in the class and referred by all objects(instances).
static variable(s) can be initialized in ini
They can be accessed only from within other static methods.
To access static variables you DO NOT need an object of the class. Just the class name is
enough to access these static members. They are also called as CLASS MEMBERS.
Mrs. Apurwa Barve
Class activity - 6 (IMCC)
You already have a Pizza class which you use to bake pizzas as per customer specification.
Let’s say you decide to count the total number of pizzas you bake in a day. For this you will
need a counter which should get incremented by 1 each time any object of Pizza class bakes
a pizza.
Also, you will need an additional method, showPizzaCount() which will return the total
number of baked pizzas.
Thus, total number of baked pizzas is a COMMON value between different objects of Pizza
class. Hence we will need to have that as a STATIC variable.
Implement the following changes in your Pizza class :
a. Add following variable and initialize it to 0.
public static int totalPizzaBaked=0;
b. Increment this variable by 1 in bakePizza() method.
c. Add a method showPizzaCount() which will return totalPizzaBaked variable.
Watch static variable video to understand static variable and above implementation.
Mrs. Apurwa Barve
static variables (IMCC)
Declaring a method static allows it to be accessed using class name only. We do not
have to create an object of that class to access/invoke that method.
Syntax to access static method :
className.methodName();
main() is a famous static method.
public static void main(String args[])
static method belongs to the class instead of the object.
It can access static data member and change its value.
Modify Pizza class and make showPizzaCount() static. It will now look like,
public static int showPizzaCount()
{
System.out.println(Pizza.totalBakedPizza);
}
We can call showPizzaCount() as follows:
Pizza.showPizzaCount();
Mrs. Apurwa Barve
static keyword - class (IMCC)
Consider,
Importing a class using ‘static’ keyword
package samples;
Can be accessed either from within the class or outside the class.
Mrs. Apurwa Barve
Inner Classes (IMCC)
//to access the inner class from within the outer class
public void accessMethod()
{
InnerClass inObj = new InnerClass();
inObj.m1(); Instantiating inner class
} object to access its members
from WITHIN the outer class
}
Mrs. Apurwa Barve
Inner Classes (IMCC)
inObjOut.m1();
} Instantiating Inner class
} object to access its
Directly accessing inner member from OUTSIDE
class member using its the outer class
instance.
Should be used instead of a local inner class when you need to use such a class only
once.
To invoke the members of this class you have to instantiate it within the method.
class LocalInnerClass
{
private int var=10; Defining local inner class
public void m1()
{
class LocalInner
{
public void readVar()
{
System.out.println("the value of var is..."+var);
}
} Instantiating the local class
LocalInner locObj = new LocalInner();
locObj.readVar();
}
Calling local class method
}
using its object
Mrs. Apurwa Barve
Inner Classes – Local inner class (IMCC)
Can access static data members of the outer class, including private members.
‘static’ keyword does not do to the class that it does to the other members for
which it is used i.e we need to create an object of the static inner class to access its
members.
Mrs. Apurwa Barve
Inner Classes – Static nested class (IMCC)
stObj.m1();
}
} Calling the static class’s
method
Mrs. Apurwa Barve
this keyword (IMCC)
this
To overcome
To refer to
instance
CURRENT object
variable hiding
Mrs. Apurwa Barve
this keyword (IMCC)
class Dog
{ //in the above print statements we access the
String breed; //properties of the current Dog
int age; //class object by using the keyword ,'this'
String colour; //followed by (.) and the variable name.
}
//Below we have a parametrised constructor which }
//takes 3 arguments. public class ThisDemo1 {
Dog(String breed,int age,String colour) public static void main(String args[])
{ {
//using this keyword we will avoid instance //we will instantiate the class Dog by passing
//variable hiding. //the necessary 3 arguments to it
this.breed = breed; //constructor.
this.age = age; Dog labDog= new Dog("Labrador",2,"Fawn");
this.colour = colour; labDog.showProperties();
} }
//the following method prints the properties of the }
//Dog object passed to it as anargument.
public void showProperties()
{
System.out.println("The properties of this dog
are as follows :");
System.out.println("Breed :"+this.breed);
System.out.println("Age :"+this.age);
System.out.println("Colour :"+this.colour);
Mrs. Apurwa Barve
this keyword (IMCC)
Step 1 : We declare a class Dog with 3 instance variables. This class has a parameterized
constructor with 3 arguments. The names of these local parameters are same as those of
instance variables.
breed breed
age
age
colour
colour
Mrs. Apurwa Barve
this keyword (IMCC)
Step 2 : We create an object of Dog class, dg, and pass 3 arguments to it. Thus we invoke the
constructor. i.e Dog dg= new Dog("Labrador",2,"Fawn");
dg labDog - constructor
colour = fawn
Mrs. Apurwa Barve
this keyword (IMCC)
Step 3 : We now need to fix the values of local variables, breed, age and colour, from within
the constructor to the instance variables, breed, age and colour of the object dg.
For this, we use the keyword ‘this’ to refer to the instance variables and assign the values
of same named local variable to instance variables.
dg - object
breed = labrador
age =2
colour = fawn
labDog() -constructor
this.breed = breed
this.age = age
this.color = colour
Mrs. Apurwa Barve
this keyword (IMCC)
If we do not use ‘this’ in the constructor then due to instance variable hiding, the
instance variables of object dg will never get the values passed as arguments. These
arguments will remain within the scope of the constructor. And instance variables will be
initialized to their default values. Hence the values do not pass from the local variables to
the instance variables. Thus it will look like below :
dg - object
breed =null
age =0
colour = null
dg() -constructor
breed=breed
age=age
colour=colour
Mrs. Apurwa Barve
Class activity - 8 (IMCC)
1. Let us code Dog class using Eclipse. Do not use this keyword first and observe which
values you see in instance variables. Now modify the code to have this keyword and
observe the change in instance variables.
Mrs. Apurwa Barve
Accessors and Mutators (IMCC)
1. As per described in the video, modify Student class to have accessor and mutator
methods and show their use.
Mrs. Apurwa Barve
Object Cloning (IMCC)
For facilitating object cloning that class needs to do 2 things: implement Cloneable
interface and override clone().
We can create a copy of object by calling new operator but that leads to extra overhead
and hence NOT preferred.
Read
https://www.tutorialspoint.com/java/java_generics.htm#:~:text=Java%20Generic%20meth
ods%20and%20generic,invalid%20types%20at%20compile%20time.
Mrs. Apurwa Barve
What we learnt? (IMCC)
Mrs. Apurwa Barve
Quiz (IMCC)
Kindly take the quiz of this chapter to assess your knowledge of concepts covered in this
chapter.
https://techvidvan.com/tutorials/java-garbage-collection/
https://stackify.com/what-is-java-garbage-collection/
https://www.javaworld.com/article/3512039/does-java-pass-by-reference-or-pass-
by-value.html
https://www.geeksforgeeks.org/static-methods-vs-instance-methods-java/
https://www.edureka.co/blog/shallow-and-deep-copy-java/
Read about cloning v/s copy constructor
https://www.baeldung.com/java-copy-
constructor#:~:text=Copy%20Constructor%20vs.&text=In%20Java%2C%20we%20can%
20also,is%20much%20easier%20to%20implement.&text=The%20clone%20method%2
0returns%20a%20general%20Object%20reference.
Thank you!