Académique Documents
Professionnel Documents
Culture Documents
management software:
Create a class Book. A book has title, publisher, author, ISBN,
no of copies. Create constructor for this class. Create methods
to issue a book, return a book.
Note- Do not bother about the person details who issued the
book, at this point of time.
Understanding static
Static member can be used by itself, without
reference to a specific instance.
When a member is declared static, it can be
accessed before any objects of its class are
created, and without reference to any object.
You can declare both methods and variables to be
static, by preceding the declaration with static
keyword.
The most common example of a static
member is main( ). main( ) is declared as
static because it must be called before any
objects exist.
Understanding static
Static members are also known as class
members as they belong to class as a whole ie.
they are shared by all the instances of the class.
A member that is not declared as staticis
implicitly (automatically) an instance member.
Class variables are shared by all instances of a
class and can be accessed through the class
name as well as an instance reference.
Instances of a class get their own copy of each
instance variable, which must be accessed through
an instance reference.
Understanding static
So,
Static => Class Member => single copy
shared by all objects=>Can be accessed via
instance reference or classname
Example: Math.pow(2,3); //accessed via classname
Non-Static => Instance Member => separate
copy for different objects=>Can be accessed
via instance reference
Example: s1.setCgpa(7.5); //accessed via instance reference
Example
public class Student {
String name;
long rollNumber;
double cgpa;
int semester;
static double avgCgpa; //class member(static)
public double getCgpa(){
return cgpa;
}
public void setCgpa(double newCg){
cgpa=newCg;
//rule1
System.out.println("updated cg="+getCgpa()); //rule1
System.out.println("avg of class="+avgCgpa); //rule2
}
public static void setAvgCgpa(double avg){
avgCgpa=avg; //rule3
cgpa=3.5;
//rule4=error
semester=3;
//rule4=error
}
}
Questions
1. Consider the following class:
public class IdentifyMyParts {
public static int x = 7;
public int y = 3;
}
a. What are the class variables?
b. What are the instance variables?
c. What is the output from the following code:
IdentifyMyParts a = new IdentifyMyParts();
IdentifyMyParts b = new IdentifyMyParts();
a.y = 5;
b.y = 6;
a.x = 1;
b.x = 2;
System.out.println("a.y = " + a.y);
System.out.println("b.y = " + b.y);
System.out.println("a.x = " + a.x);
System.out.println("b.x = " + b.x);
System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);
Questions
2. What's wrong with the following program?
public class SomethingIsWrong {
public static void main(String[] args) {
Rectangle myRect;
myRect.width = 40;
myRect.height = 50;
System.out.println("myRect's area is " + myRect.area());
}
}
Constructor Overloading
Defining more than 1 constructor in a
class is known as constructor overloading.
Constructor Overloading
What if the signature of each constructor
definition is different?
Example
public class Student {
String name; long rollNumber; double cgpa; int semester;
public Student(){
//default(no-args) constructor
name="abc";
}
public Student(String n,long rn, double cg, int sem ){
//parameterized full initailization- initializing all the
instance variables
name=n;
rollNumber=rn;
cgpa=cg;
semester=sem;
}
public Student(String n, long rn, double cg){
//parameterized partial initailization- but initializing some
the instance variables
name=n;
rollNumber=rn;
cgpa=cg;
semester=2;
}}