Vous êtes sur la page 1sur 13

Write a Java class to store following information for a library

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.

Till Now weve learned..


Class is:
a user-defined datatype.
a template for real-world objects.
defines state (via instance variables) and
behavior (via instance methods) of object.
Object is instance of class.
Instance variables will have different value for
different objects.
Usually, Instance variables are accessed (or
manipulated) using instance methods.
Constructors are used to initialize object at the time

Till Now weve learned..


Once object is created, instance member (variable
or method) can be accessed with object of the class.
example:
Student s1=new Student(); //creating object
s1.name=Ankit;
// accessing instance member
s1.getCgpa();
//accessing instance method

However, sometimes we want to define a class


member that will be used independently of any
object of that class.
example: avgCgpa is independent of any object.
avgCgpa should be defined at class level (static)

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

Some rules for static members


Instance methods can access instance variables
and instance methods directly. (rule1)
Instance methods can access class variables and
class methods directly. (rule2)
Class methods can access class variables and
class methods directly. (rule3)
Class methodscannotaccess instance variables
or instance methods directlythey must use an
object reference. Also, class methods cannot use
thethiskeyword as there is no instance forthisto
refer to. (rule4)
In other words, a non-static member cannot
be directly accessed from static context.

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.

Just think for a while, constructor has same


name as that of class. And in Java, we cannot
have more than 1 variables with same name or
more than 1 method with same name.
How is it possible to define more than 1
constructor, all having same name (same
as name of class)?
Try to think!!

Constructor Overloading
What if the signature of each constructor
definition is different?

What is signature of a constructor?


Signature (of a method or constructor)
includes 3 things:
number of paramaters
datatype of parameters
order of parameters
Yes we can declare more than 1 constructor,
although having same name but having
different signature.

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;
}}

Vous aimerez peut-être aussi