Java Inner Classes, Exercises of Java Programming

Inner classes: classes that you can write inside another class. Common applications include iterators and GUIs. Enums: define named constants (e.g., a type ...

Typology: Exercises

2022/2023

Uploaded on 03/01/2023

unknown user
unknown user 🇺🇸

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Page 1
Java Inner Classes
David I. Schwartz
COMS/ENGRD 211
Java Inner Classes Step 1 Page 2/12
Step 1: Class Declarations
1.1 Non-Generic
modifiers class classname extends-clause implements-clause {
fields
enums
initializers
constructors
methods
classes
interfaces
}
Members:
fields
methods
enums
classes
interfaces
Note that members can be
static
.
1.2 New Concepts
What you need to know:
Inner classes: classes that you can write inside another class. Common
applications include iterators and GUIs.
Enums: define name d constants (e.g., a type called
Color
that has values
BLUE
,
RED
, …). We will save enums for another document.
What you don’t really need to know:
Inner interfaces
: Yes, you can really write an interface inside a c lass. The rules get
complex. Save for a really, really rainy day.
Initializers: We tend not to cover them, but they’re a ctually rather useful and help
to hint at anonymous classes. Imagine using a method body without a header. Why
bother? You might wish to set data when creating an object for the first time.
Rather than calling a method, you can use a statement block to set the data.
pf3
pf4
pf5

Partial preview of the text

Download Java Inner Classes and more Exercises Java Programming in PDF only on Docsity!

Page 1

Java Inner Classes

David I. Schwartz

COMS/ENGRD 211

Java Inner Classes

Step 1

Page 2/

Step 1:

Class Declarations

modifiers Non-Generic

class

classname

extends-clause

implements-clause

interfacesclassesmethodsconstructorsinitializersenums fields

Members: }

fields

methods

enums

classes

interfaces

Note that members can be

static

What you need to know: New Concepts

Inner

classes

classes

that

you

can

write

inside

another

class.

Common

applications include iterators and GUIs.

Enums

: define named constants (e.g., a type called

Color

that has values

BLUE

RED

, …). We will save enums for another document.

What you don’t really need to know:

Inner interfaces

: Yes, you can really write an interface inside a class. The rules get

complex. Save for a really, really rainy day.

Initializers

: We tend not to cover them, but they’re actually rather useful and help

Rather than calling a method, you can use a statement block to set the data.bother? You might wish to set data when creating an object for the first time.to hint at anonymous classes. Imagine using a method body without a header. Why

Java Inner Classes

Step 1

Page 3/

public Initializer example:

class Initializers

public

static

void

main(String[]

args)

new

Test().print1();

output:

new

Test().print2();

output:

class}

Test

public

final int

N=10;

private

int[]

x=new

int[N];

for

(int

i=0;

i<N;

i++)

x[i]=i;

public

static

final

int

L=5;

private

static

int[]

y=new

int[L];;

static

for (int

i=0;

i<L;

i++)

y[i]=i;

public

void print1()

for

(int

i=0;

i<

x.length;

i++)

System.out.print(x[i]);

System.out.println();

public

void

print2()

for

(int

i=0;

i<

y.length;

i++)

System.out.print(y[i]);

System.out.println(); }

modifiers Generic class syntax: You can write a class or interface that serves as a template to make other classes. Generic Classes and Interfaces

class

classname

Type

TypeN

baseclause

classbody

We will not deal with generic classes at this point. }

Java Inner Classes

Step 2

Step 2:

Levels of Classes

Top-Level (or Outer) Class

You can put a class inside an another class.

A class that contains other classes is a

TLC

The classes you have seen up until now are TLCs.

Nested class Nested Class

Class declared inside another class.

Two kinds of nested classes:

Member class

: class declared at the member-level of a TLC.

Local class

: class declared inside a method, constructor, or initializer block.

Inner class Inner Class

(IC) refers to two special kinds of nested class:

Non-static member class (member class with no

static

modifier).

Local class inside a non-static member of a TLC.

Why called inner class?

Because an object made from the class will contain a reference to the TLC.

Use

TLC

.this.

member

from inside inner class to access member of TLC.

Restrictions:

Inner class fields can be

static

, but then must also be

final

No

static

methods or other inner classes (same for other members?)

See language references for even more details.

Handy way to think of inner classes inside a TLC:

At the member level:

  • called- just like a variable or method.

member class

At the statement level:

  • called- just like a statement in a method

local class

At the expression level:

  • called- just like an expression

anonymous class

Java Inner Classes

Step 3

Page 7/

class

OC

private

int x

private

int y

public

class IC

private

int x

private

int y

public

void print()

//System.out.println(this.x+OC.this.x);

System.out.println(y+this.y);

method print

class

IC

class

OC

public Example

class Memberclass

public

static

void

main(String[]

args)

new

OC().new IC().print();

Output:

IC,
OC

class}

OC

public

class IC

public

String

toString()

return

"IC";

public

void print()

System.out.println(OC.this);System.out.println(this);

public}

String

toString()

return

"OC";

Java Inner Classes

Step 3

public Example

class Memberclass

public

static

void

main(String[]

args)

new

OC2().new

IC().print();

output:

class}

OC

public

class IC

private

int x

public

void print()

System.out.println(x);

class}

OC

extends

OC

Java Inner Classes

Step 4

Page 9/

Step 4:

Local Classes (Statement Level)

Local class location: Rules

Statement level declaration.

Usually written in methods. See also constructors and initializers.

Scope:

Local to block.

Can access all members of the TLC.

Actually, things can get confusing here!

  • Java does have rules for dealing with the matter.- An object of local class might persist after method ends.

Example structure:

public

class

TLC

methodheader tlc_members

public statements

class

InnerClass

ic_members

statements }

moreTLCmethods }

More restrictions:

Cannot be used outside of block.

No modifiers.

Enclosing block’s variables must be

final

for local class to access.

No

static

, but can have

static

final

(constants).

Terminate with a semicolon! The class is effectively an expression statement.

Cannot have same name of TLC.

Java Inner Classes

Step 4

public Example

class LocalClass

public

static

void

main(String[]

args)

new

OC().print();

class}

OC

public

void print()

final

String s

"test:

class

Point {

private

int x;

private

int y;

public

Point(int

x,int

y)

this.x=x;

this.y=y;

public

String

toString()

return

s+"("+x+","+y+")"; }

System.out.println(new};

Point(1,2));

method print

class

OC