4. OOP in Java - Part 2
4. OOP in Java - Part 2
Java
static keyword
To define a class member that will be used independently of any object of
that class
When a member is declared static, it can be accessed before any objects of
its class are created, and without reference to any object.
Can declare both methods and variables to be static
Instance variables declared as static are, essentially, global variables.
When objects of its class are declared, no copy of a static variable is made.
Instead, all instances of the class share the same static variable.
Methods declared as static have several restrictions:
They can only directly call other static methods of their class.
They can only directly access static variables of their class.
They cannot refer to this or super in any way.
if you wish to call a static method from outside its class, you can do so
using the following general form:
classname.method( )
2
static keyword
3
Introducing Access Control
Encapsulation provides another important attribute:
Access control.
Through encapsulation, you can control what parts of a
program can access the members of a class.
By controlling access, you can prevent misuse.
How a member can be accessed is determined by the access
modifier attached to its declaration.
Some aspects of access control are related mostly to
inheritance or packages. (A package is, essentially, a grouping
of classes.)
Java’s access modifiers are public, private, and protected.
Java also defines a default access level.
protected applies only when inheritance is involved. 4
Introducing Access Control
In the classes developed so far, all members of a class have used
the default access mode.
Sometimes we want to restrict access to the data members of
a class— allowing access only through methods.
Also, there will be times when you will want to define
methods that are private to a class.
An access modifier precedes the rest of a member’s type
specification - is, it must begin a member’s declaration
statement. public int i;
private double j;
private int myMethod(int a, char b) { //...
5
The access of either a class or a member or both in java can be declared with the
help of Access Modifiers.
Public Class
Must
Must
May
May
1
Non Access Modifiers
1
Nested and Inner Classes
There are two types of nested classes: static and non-static
A static nested class is one that has the static modifier applied
Because it is static, it must access the non-static members of its enclosing
class through an object - it cannot refer to non-static members of its enclosing
class directly
Because of this restriction, static nested classes are seldom used.
The most important type of nested class is the inner class
An inner class is a non-static nested class
It has access to all of the variables and methods of its outer class and may
refer to them directly in the same way that other non-static members of the
outer class do.
It is also possible to declare a nested class that is local to a block.
1
inner classes within any block
scope
18
Use of CommandLine Args
21
Overloading Vararg Methods
//Varargs and
overloading.
class VarArgs3
{
static void
vaTest(int
... v)
{
System.out.print("vaTest(int ... ): "+ "Number of args:
" + v. length + "Contents: "); for(int x : v)
System. out. print
(x + " ") ;
System.out.println();
}
static void
vaTest(boolean ... v)
{
System.out.print("vaTest(boolean ... ) " + "Number of
args: " + v. length + "Contents: "); for(boolean x : v)
System. out. print
(x + " ") ;
System.out.println();
}
static void vaTest(String
msg, int ... v)
{
System.out.print("vaTest(String, int ... ):"+
msg + v.length + "Contents: "); for ( int x : v)
System. out. print
(x + " ") ;
System.out.println();
}
public static void
main(String args[])
{
vaTest(1, 2, 3);
vaTest(“Testing”,
10, 20); 22
23
Type Inference with Local
Variables
Recently, an exciting new feature called local variable type inference was added to
the Java language.
Beginning with JDK 10, it is now possible to let the compiler infer the type of a local
variable based on the type of its initializer, thus avoiding the need to explicitly
specify the type
local variable type inference has become a common part of the
contemporary programming environment
type inference, the context-sensitive identifier var was added
var counter; // Wrong! Initializer required.
var avg = 10.0;
int var = 1; // In this case, var is simply a user-defined identifier.
var myArray = new int[10]; // This is valid.
var[] myArray = new int[10]; // Wrong
var myArray[] = new int[10]; // Wrong
var myArray = { 1, 2, 3 }; // Wrong
Only one variable can be declared at a time;
a variable cannot use null as an initializer; and the variable being declared
cannot be used by the initializer expression
24
Local Variable Type Inference with
Reference Types
Earlier examples have shown type inference with primitive types, but it
can also be used with reference types.
var myStr = "This is a string";
one of the benefits of local variable type inference is its ability to streamline
code, and it is with reference types where such streamlining is most
apparent.
The reason for this is that many class types in Java have rather long names.
FileInputStream fin = new FileInputStream("test.txt");
With the use of var, it can now be written like this:
var fin = new FileInputStream("test.txt");
25
Local Variable Type Inference with
Reference Types
26
Local Variable Type Inference with
Reference Types
27
Garbage Collection
Since objects are dynamically allocated by using the new operator, you might
be wondering how such objects are destroyed and their memory released for
later reallocation
In some languages, such as traditional C++, dynamically allocated objects
must be manually released by use of a delete operator
Java takes a different approach; it handles deallocation for you automatically
The technique that accomplishes this is called garbage collection
It works like this: when no references to an object exist, that object is assumed to
be no
longer needed, and the memory occupied by the object can be reclaimed
There is no need to explicitly destroy objects
Garbage collection only occurs sporadically (if at all) during the execution of
your program
It will not occur simply because one or more objects exist that are no longer used
Furthermore, different Java run-time implementations will take varying
approaches to garbage collection, but for the most part, you should not have to think
about it while writing your programs
28
Object Lifetime and Garbage
collection
Object Lifetime and garbage collection
29