Static
Static
Static
/
When static??
When a methods behaviors has no dependency on
state of an object
i.e.., when the method will never be instance specific.
E.g.., A class having a method which generates
random number
/
Static method:
JVM doesnt have to create an instance of the
class just to start running of the code.
Eg., public static void main(String args[])
Static variables and methods belong to a class
rather than to any particular instance.
/
class hello //usage of static
{
static int count=6;
public hello()
{
count+=1;
}
public static void main(String args[])
{
new hello();
new hello();
new hello();
System.out.println("Value of count :"+ count);
}
}
/
class hello //usage of non-static
{
int count=6;
public hello()
{
count+=1;
}
public static void main(String args[])
{
new hello();
new hello();
new hello();
System.out.println("Value of count :"+new hello().count);
}
}
/
Properties:
Non-static
method can access a static
method also variables
/
Accessing static methods
and variables:
Since we dont need to have an
instance ,How can we invoke a static
method or variable without using it??
/
Class Fruits{
Static int fruitcount=1;
Public Fruits() {
fruitcount +=1;
}
}
Class TestFruit
Public static void main(String args[])
{
new Fruits(); new Fruits();
System.out.println(fruit count: +
Fruits.fruitcount);
}
} o/p:fruit count:3
/
Class Fruits{ //wat if we use object
reference variable??
Static int fruitcount=1;
Public Fruits() {
fruitcount +=1;
}
}
Class TestFruit{
Public static void main(String args[])
{
Fruits f =new Fruits();
Intfruit=f.fruitcount; // reference
variable-f
System.out.println(fruit count: + fruit);
} } o/p:fruit count:2
/
We use only the object reference variable but
not the object it refers
/
Coupling and cohesion:
Good OO design calls for
/
Coupling:
Degree to which one class knows about another
class
Key benefits:
Classes are much easier to maintain
than classes with low cohesion
Classes with a well-focused purpose tend
to be more
reusable than other classes
/
Example:
class BudgetReport {
void connectToRDBMS(){ }
void generateBudgetReport() { }
void saveToFile() { }
void print() { }
}
/
class BudgetReport {
Options getReportingOptions() { }
void generateBudgetReport(Options o)
{}
}
class ConnectToRDBMS {
DBconnection getRDBMS() { }
}
class PrintStuff {
PrintOptions getPrintOptions() { }
}
class FileSaver {
SaveOptions getFileSaveOptions() { }
}
/
Select the two statements that best indicate
a situation with low coupling. (Choose two.)
A. The attributes of the class are all private.
B. The class refers to a small number of
other objects.
C. The object contains only a small number
of variables.
D. The object is referred to using an
anonymous variable, not directly.
E. The reference variable is declared for an
interface type, not a class. The interface
provides a small number of methods.
F. It is unlikely that changes made to one
class will require any changes in another.
/
Answer:
E and F are correct. Only having access to a small number of
methods implies limited coupling.
If the access is via a reference of interface type, it may be argued
that there is even less opportunity for coupling as the class type
itself is not visible.
Stating that change in one part of a program are unlikely to cause
consequences in another part is really the essence of low coupling.
There is no such thing as an anonymous variable.
Referring to only a small number of other objects might imply low
coupling, but if each object has many methods, and all are used,
then coupling is high.
Variables (attributes) in a class should usually be private, but this
describes encapsulation, rather than low coupling. Of course,
good encapsulation tends to reduce coupling as a consequence.
/
Which statement(s) are true? (Choose all that
apply.)
A. Cohesion is the OO principle most closely
associated with hiding implementation details.
B. Cohesion is the OO principle most closely
associated with making sure that classes know
about other classes only through their APIs.
C. Cohesion is the OO principle most closely
associated with making sure that a class is
designed with a single, well-focused purpose.
D. Cohesion is the OO principle most closely
associated with allowing a single object to be
seen as having many types.
/
Answer:
Answer C is correct.
A refers to encapsulation, B refers to
coupling, and D refers to
polymorphism.
/
Thank You