CPE207 Object Oriented Programming (Week 5)
CPE207 Object Oriented Programming (Week 5)
2
3
If any variable, we declared as static is known as static variable.
The static variable allocate memory only once in class area at the
time of class loading.
4
Suppose we want to store record of all employee of
any company, in this case employee id is unique for
every employee but company name is common for
all. So, use static variable to store the company name.
When we create a static variable as a company name
then only once memory is allocated otherwise it
allocate a memory space each time for every
employee object.
5
6
static keyword always fixed the memory that means that will be
located only once in the program
final keyword always fixes the value that means it makes
variable values constant
Note: As for as real time statement there concern every final
variable should be declared the static but there is no obligation
that every static variable declared as final.
7
When a variable is declared
with final keyword, its value
can’t be modified, essentially,
a constant.
initializing
This also means that you
must initialize a final
variable.
◦ If you cannot change it then you
must initialize it, right!
8
Static method is a method which belongs to the
class and not to the object(instance)
A static method can access only static data. It can
not access non-static data (instance variables)
A static method can call only other static
methods and can not call a non-static method
from it.
A static method can be accessed directly by the
class name and doesn’t need any object
A static method cannot refer to "this“ keyword.
9
10
11
class MyClass{ public class JavaApp {
private static int data;
public static void main(String[] args) {
public static void setData(int d) {
MyClass.setData(50);
data = d;
} System.out.println(MyClass.getData());
}
public static int getData() { }
return data;
}
}
No need to create an
object
12
We know the static keyword now.
13
The Java allows you to define a class class OuterClass {
within another class. Such a class is ...
called a nested class static class
Nested classes are divided into two StaticNestedClass {
categories: ...
}
◦ non-static. Non-static }
nested classes are called
inner classes. class OuterClass {
◦ Static: Static nested classes ...
are called static nested class InnerClass {
...
classes }
}
14
As with instance methods and variables, an class OuterClass {
inner class is associated with an instance of ...
its enclosing class(outer) and has direct class InnerClass {
access to that object's methods and ...
variables. }
}
Also, because an inner class is associated
with an instance, it cannot define any static
members itself.
Inner classes
OuterClass outerObject = new OuterClass(); //you need to create an object first
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
15
As with class methods and
variables, a static nested class is class OuterClass {
associated with its outer class. ...
static class
And like static class methods, a StaticNestedClass{
static nested class cannot refer ...
directly to instance variables or }
}
16
•If a class is useful to only one other class, then it is logical to embed it in that
class and keep the two together.
https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
17
class OuterClass {
int x = 10;
18
class OuterClass {
int x = 10;
19
Syntax
An enum type is a special data type that enables
for a variable to be a set of predefined enum Day {
constants. SUNDAY,
The variable must be equal to one of the values MONDAY,
that have been predefined for it. TUESDAY,
Common examples include compass directions WEDNESDAY,
(values of NORTH, SOUTH, EAST, and WEST), the
THURSDAY,
days of the week and so on.
FRIDAY,
SATURDAY
}//simple enum
20
A Java enum type can have a private constructor that can be used to
initialize instance variables(attributes).
public enum Branch { values() method can be used
MATH(001), to return all values present
PHYSICS(002),
GEOMETY(003)
inside enum
; // semicolon needed when fields / methods follow
21
enum CompanyName{
GOOGLE(1995, "Google was founded in 1998 by Larry Page and Sergey Brin while"),
MICROSOFT(1975, "Microsoft Corporation is a technology company with headquarters in
Redmond, Washington");
} 22
23
24
Create a Circle class where
◦ declare a private constant double variable PI has value
of 3,141519
◦ declare a private variable called radius.
◦ class constructor will have an argument to set radius.
◦ declare a method called computeArea() to compute area
of a circle object.
◦ create 3 different circle instances, with radiuses 5, 10,
15.
◦ print all the areas using a foreach loop.