0% found this document useful (0 votes)
50 views

15 Javainnerclass

An inner class is a class declared within another class. To access members of an inner class, an object instance of the inner class must first be created. Methods of an inner class can directly access members of the outer class that contains it. An inner class can access data members and call methods of its outer class, while the outer class must create an instance of the inner class to access its members.

Uploaded by

api-3748699
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

15 Javainnerclass

An inner class is a class declared within another class. To access members of an inner class, an object instance of the inner class must first be created. Methods of an inner class can directly access members of the outer class that contains it. An inner class can access data members and call methods of its outer class, while the outer class must create an instance of the inner class to access its members.

Uploaded by

api-3748699
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Inner Class

1
What is an Inner Class?
● Class declared within another class

● Accessing the members of the inner class:


– Need to instatiate an object instance of an inner class
first
– Example:
innerObj.innerMember = 5;
//innerObj is an instance of the inner class
//innerMember is a member of the inner class

2
Accessing Members of Outer class
within an Inner class
● Methods of the inner class can directly access
members of the outer class
– Example:
1 class Out {
2 int OutData;
3 class In {
4 void inMeth() {
5 OutData = 10;
6 }
7 }
8 }

3
Java Program Structure: Inner
Classes
1 class OuterClass {
2 int data = 5;
3 class InnerClass {
4 int data2 = 10;
5 void method() {
6 System.out.println(data);
7 System.out.println(data2);
8 }
9 }
10

11 //continued...

4
Java Program Structure:
9 public static void main(String args[]) {
10 OuterClass oc = new OuterClass();
11 InnerClass ic = oc.new InnerClass();
12 System.out.println(oc.data);
13 System.out.println(ic.data2);
14 ic.method();
15 }
16 }

5
Java Inner Class

You might also like