LAB (8)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

JAVA FINALIZE, INNER &

NESTED CLASSES
Lab-08

Submitted by: Syed Muhammad Ali ( INFT231102007)


Ali Haider ( INFT231102013)
Ather Nazeer ( INFT231102027)
Ateeb Nazeer ( INFT231102028)
Hussain Ali Gill ( INFT231102044)
Daniyal Shahzad ( INFT231102058)
GROUP NO: 06
LAB MANUAL: 08
LAB 08 Finalize, Nested & Inner Classes

Lab Objectives:
1. Understanding the concept of garbage collection
2. Understanding and implementing Nested and Inner classes
3. Understanding and Implementing Encapsulation

Software Required:
JDK & Notepad/ Textpad

The finalize( ) Method

Sometimes an object will need to perform some action when it is destroyed. For example, if an
object is holding some non-Java resource such as a file handle or character font, then you might
want to make sure these resources are freed before an object is destroyed. To handle such
situations, Java provides a mechanism called finalization. By using finalization, you can define
specific actions that will occur when an object is just about to be reclaimed by the garbage
collector.

TASK 1: See Output of the following Program

class A {
int i = 50;
@Overrideprotected void finalize() throws Throwable {
System.out.println("From Finalize Method"); }
}

public class Test


{
public static void main(String[] args)
{
//Creating two instances of class A
A a1 = new A();
A a2 = new A();
//Assigning a2 to a1
a1 = a2;
//Now both a1 and a2 will be pointing to same object
//An object earlier referred by a1 will become

//abandoned

System.gc();System.out.println("done");

final Keyword: The final keyword can be applied with the variables, a final variable that has
no value it is called blank final variable or uninitialized final variable. It can be initialized in the
constructor only.
TASK 2: Value of final variable cannot be changed once declared. Run the
following code and check output.

class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[])
{
Bike9obj=new Bike9(); obj.run();
}
}//end of class
TASK 3: Try to initialize value of final variable in a constructorTASK 4: Do
not initialize final variable on declaration and try to initialize it anywhere
other than constructor.

TASK 4: Declare final variable as static and try to initialize its value in a
method. Then create a static block and initialize the static final variable value.
Nested and Inner Classes
TASK 5: Run the following code and try to understand its working

// Demonstrate an inner class.


class Outer {
int outer_x = 100;
void test() {
Inner inner = new Inner();
inner.display();
}
// this is an inner class
class Inner {
void display() { System.out.println("display:
outer_x = " + outer_x); }

}
class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
} }

TASK 6: Declare a local variable in inner class and try to display it in outer
class.

TASK 7: Declare a private variable in outer class and try to display it in inner
class.
TASK 8: Try to declare inner class within a method of outer class.

TASK 9: Run and Learn from following code:

public class Outer {


static class Nested_Demo {
public void my_method() {

System.out.println("This is my nested class");

} }

public static void main(String args[])


{

Outer.Nested_Demo nested = new Outer.Nested_Demo();


nested.my_method();

} }

QUESTIONS

Please Fill the blank space with respective answers to following questions:

Question 1: What is the purpose of garbage collector in java?

ANS: The purpose of the garbage collector in Java is to automatically manage


memory by reclaiming memory occupied by objects that are no longer
reachable or in use by the program. This process helps prevent memory leaks
and allows developers to focus on writing code without worrying about
manual memory management.

Question 2: If the final variable is initialized at the time of declaration then


can it be re-initialized in constructor?
ANS: No, if a final variable is initialized at the time of declaration, it cannot
be re-initialized in the constructor or anywhere else in the code. Once a final
variable is initialized, its value cannot be changed.

Question 3: Can final variable be initialized somewhere other then


constructor?

ANS: Yes, a final variable can be initialized somewhere other than the constructor, but it
must be initialized exactly once. This could be done in an instance initializer block, a static
initializer block, or directly at the point of declaration.

Question 4: What will be the output of the program?


class Output
{
final static short i = 2;
public static int j = 0;
public static void main(String [] args)
{
for (int k = 0; k < 3; k++)
{
switch (k)
{
case i: System.out.print(" 0 ");
case i-1: System.out.print(" 1 ");
case i-2: System.out.print(" 2 ");
}
}
}
}
Question 5: What will be the output of the following program?
public class Final
{
final int assign = 30;
public static void main(String[] args)
{
final int result = 20;
final int assign;
Final f = new Final();
assign = 20;
System.out.println(assign);
System.out.println(f.assign);
System.out.println(f.process(result));
}
int process(int a)
{
return a + 5;
}
}
THE END

You might also like