Methods and Classes
The finalize( ) Method
class TestFinalyze
{
public static void main(String a[])
{
TestFinalyze p=new TestFinalyze();
p=null;
System.gc();
}
protected void finalize()
{
System.out.println("hi from finalyze");
}
}
Output
D:\java2016\program>java TestFinalyze
hi from finalyze
Overloading Methods or method overloading.
- define two or more methods within the same class that share the same
name, as long as their parameter declarations are different.
// Automatic type conversions apply to class Overload
overloading.
{
class OverloadDemo { public static void main(String args[])
void test() { {
System.out.println("No parameters"); OverloadDemo ob = new OverloadDemo();
} int i = 88;
// Overload test for two integer parameters. ob.test();
void test(int a, int b) { ob.test(10, 20);
System.out.println("a and b: " + a + " " + b);
ob.test(i); // this will invoke test(double)
} ob.test(123.2); // this will invoke test(double)
// Overload test for a double parameter }
void test(double a) }
{ -----------------------------------------------------------
System.out.println("Inside test(double) a: " +a); ob.test(double);//won’t invoke ob.test(int)
} Overload.java:30: error: incompatible types:
} possible lossy conversion from double to int
• // Primitive types are passed by value. // Objects are passed through their references.
class Test {
class Test {
int a, b;
void meth(int i, int j) { Test(int i, int j)
i *= 2; {
j /= 2; a = i;
b = j;
}
}
} // pass an object
class CallByValue { void meth(Test o)
public static void main(String args[]) {
o.a *= 2;
{
o.b /= 2;
Test ob = new Test(); }
int a = 15, b = 20; }
System.out.println("a and b before call: " +a + " "
+ b); class PassObjRef {
public static void main(String args[])
ob.meth(a, b);
{ Test ob = new Test(15, 20);
System.out.println("a and b after call: " +a + " " + System.out.println("ob.a and ob.b before call: " +
b); ob.a + " " + ob.b);
}
} ob.meth(ob);
System.out.println("ob.a and ob.b after call: " +
output
ob.a + " " + ob.b);
a and b before call: 15 20 }
a and b after call: 15 20 }
output:
ob.a and ob.b before call: 15 20
Static Members – Instance members
• certain members should only • Each object created will have
belong to the class, and not be its own copies of the fields
part of any object created from and methods defined in its
the class. class
• Static variables • Instance variables
– Variables belong to the class – fields of an object are called
and not belongs to any instance variables
object of the class. – values of the instance
variables in an object
– initialized when the class is
comprise its state
loaded at runtime
• Instance methods
• Static methods – methods of an object are
– Methods belong to the class called instance methods
and not belongs to any • Instance members
object of the class – Instance variables and
• Static members instance methods, which
– Static variables and static belong to objects, are
collectively called instance
methods are collectively
members
known as static members
• If the instance variables of
and are declared with a
two distinct objects have same
keyword “static”
value, then these two objects
are in the same state.
• When a member is declared static, it can be
accessed before any objects of its class are
created, and without reference to any object.
• Methods declared as static have several
restrictions:
– They can only directly call other static methods.
– They can only directly access static data.
– They cannot refer to this or super in any way.
Program 1: Program 2:
class sta
class UseStatic {
{
static int a = 3; static
static int b; { System.out.println("hello"); }
static void meth(int x) { }
class UseStatic
System.out.println("x = " + x); {
System.out.println("a = " + a); static int a = 3; static int b;
System.out.println("b = " + b); static void meth(int x) { System.out.println("x = " + x);
} System.out.println("a = " + a);
System.out.println("b = " + b);}
static { static {
System.out.println("Static block initialized."); System.out.println("Static block initialized.");
b = a * 4; b = a * 4;
}
}
public static void main(String args[]) {
public static void main(String args[]) { sta obj=new sta();
meth(42); meth(42); } }
} } Static block initialized.
hello //sta obj=new sta(); this statement
Static block initialized. not there hello wont be printed
x = 42 x = 42
a=3 a=3
b = 12
b = 12
to call a static methods from outside of its class, we can call by their class name
classname.method( )
class StaticDemo
{ static int a = 42;
static int b = 99;
static void callmestatic()
{
System.out.println("a = " + a);
}
}
class StaticByName {
public static void main(String args[])
{ StaticDemo.callmestatic();
System.out.println("b = " + StaticDemo.b);
}
}
output:
a = 42 b = 99
class sta
class UseStatic
{
{
static static int a = 3; static int b;
{ System.out.println("hello"); static void meth(int x) {
} System.out.println("x = " + x);
sta() System.out.println("a = " + a);
{ System.out.println("b = " + b);
}
System.out.println("Constructor");
static
}
{
} System.out.println("Static block initialized.");
b = a * 4;
Output: }
Static block initialized. public static void main(String args[])
hello {
sta obj=new sta();
Constructor
meth(42);
x = 42
}
a=3 }
b = 12
class StaticDemo class StaticByName {
{ static int a = 42; public static void main(String args[])
static int b = 99; {
static void callmestatic() StaticDemo.callmestatic();
{ System.out.println("b = " + StaticDemo.b);
System.out.println("a = " + a); }
} }
static Output:E:\javaprogram>java StaticByName
{ StaticDemo static block
System.out.println("StaticDemo static a=42
block"); b = 99
}
}
final variable
prevents its contents from being modified(constant)
We must initialize a final field when it is declared.
Two ways are there to initialize.
• One:assign a value when it is • Two :assign a value within a
declared. constructor
class Final class Final
{ {
final int i=30; final int i;
Final() Final()
{ {
i=30;
}
}
public static void main(String a[])
{
public static void main(String a[])
Final f=new Final(); {
System.out.println(f.i); Final f=new Final();
} System.out.println(f.i);
} }
class Final
{
final int i;
Final()
{
}
void fn()
{
i=20;
}
public static void main(String a[])
{
Final f=new Final();
f.fn();
System.out.println(f.i);
}
}
error: cannot assign a value to final variable i
i=20;
^
1 error
arrays: they are implemented as objects.
there is a special array attribute :length instance variable.
// This program demonstrates the length array member.
class Length {
public static void main(String args[]) {
int a1[] = new int[10];
int a2[] = {3, 5, 7, 1, 8, 99, 44, -10};
int a3[] = {4, 3, 2, 1};
System.out.println("length of a1 is " + a1.length);
System.out.println("length of a2 is " + a2.length); System.out.println("length
of a3 is " + a3.length);
}
}
output:
length of a1 is 10
length of a2 is 8
length of a3 is 4
Nested class and Inner class
• Define a class with in another class is called nested class
class
{……..
class Nestedclass
{
…..........
}
}
• Static nested class • Non static nested(Inner)
class outerclass class outerclass
{ {
……………. …………………..
static class StaticNestedClass class InnerClass
{ {
…………….. …………………
} }
} }
• Member Inner class- A class defined within a
class and outside of the method
• Local Inner class : A class defined with in a
method
• Anonymous Inner class: A class that have no
name –It is used when we override method of
class or interface
Member Inner class
class MemberInnerClass
{ private int data=30;
class InnerClass
{
void msg()
{
System.out.println(data);
}
}
public static void main(String args[])
{
MemberInnerClass obj=new MemberInnerClass ();
MemberInnerClass.InnerClass in=obj.new InnerClass();
in.msg();
obj.msg();//error: cannot find symbol
}
}
Class files
MemberInnerClass.class
MemberInnerClass$InnerClass.class
To instantiate InnerClass we must have to create the instance of outer class
Local Inner class
class LocalInner
{ private String data="outerdata";
void outerFn()
{
class InnerClass
{
void innerFn()
{
System.out.println("innerclass"+data);
}
}
InnerClass i=new InnerClass();
i.innerFn();
}
public static void main(String args[])
{ LocalInner obj=new LocalInner();
obj.outerFn();}}
Class files:
LocalInner$1InnerClass.class
LocalInner.class
Anonymous Inner class
class Person class Person
{ } { void fn()
class TestAnonymousInner {}
}
{
class TestAnonymousInner
public static void main(String a[]){
{
Person p=new Person() public static void main(String a[]){
{}; Person p=new Person()
Person p1=new Person() { void fn()
{}; { System.out.println("hi");}
}} };
p.fn();
Here two anonymous inner class are created.
Both are inherited from Person class. }
}
we can assign the reference of derived class
Here derived class reference object to the base class
object to the base class reference
reference .
(p,p1 are base class person type)
Hence we can call overridden member in derived class
Three class files are created by using base class reference.
TestAnonymousInner$1.class output
TestAnonymousInner$2.class D:\java2016\program>java TestAnonymousInner
TestAnonymousInner.class hi
class Person //we cannot call the method by using base class
reference with out overridden the method
{ void fn()
{}
}
class TestAnonymousInner
{ public static void main(String a[]){
Person p=new Person()
{ void fn1()
{ System.out.println("hi");}
};
p.fn1();
}
}
D:\javalab2017\Inheritance>javac TestAnonymousInner.java
TestAnonymousInner.java:12: error: cannot find symbol
p.fn1();
^
symbol: method fn1()
location: variable p of type Person
1 error