<< Chapter < Page Chapter >> Page >

The screen output The code in Listing 16 produces the screen outputshown in Figure 9

Important considerations The important things to note about the output shown in Listing 16 include:

  • The second object instantiated from local class B is definitely a different object from the first object instantiated from local class B , as evidenced by a different value for bTime . (Compare the value of bTime in Figure 9 with the value of bTime in Figure 5 .) In other words, the second object was instantiated after the first object wasinstantiated.
  • Even though the two objects instantiated from local class B are different objects, they both belong to the same containing object, asevidenced by the same values for aTime in Figure 9 and Figure 5 .
  • The two objects instantiated from local class B each access the same final local variable belonging to the method named meth , as evidenced by the same values for methTime in Figure 9 and Figure 5 .

Run the program

I encourage you to copy the code from Listing 17 . Compile the code and execute it. Experiment with the code,making changes, and observing the results of your changes. Make certain that you can explain why your changes behave as they do.

Summary

In addition to a number of other items, a class definition can contain:

  • Member classes
  • Local classes
  • Anonymous classes
  • Nested top-level classes and interfaces

Member classes were explained in the previous module. This module explains local classes. The next module will explain anonymous classes.

A local class is a class that is defined within a block of Java code. While local classes are probably mostfrequently defined within method and constructors, they can also be defined inside static initializer blocks and instance initializers.

An object of the local class must be internally linked to an object of the enclosing class (which I often refer to as the containing object). A local class is truly an inner class because an object of the local class cannot exist in theabsence of an object of the enclosing class.

The methods of a local class have direct access to all the members in the hierarchy of enclosing classes,including private members. In addition, the methods of local classes have access to final local variables and final method parameters in the scope in which the local class is defined.

The containment hierarchy of local classes is independent of the inheritance hierarchy. However, it is technically possible toestablish an inheritance relationship between a local class and one of its enclosing classes.

Local classes may not be declared public , protected , private , or static .

Local classes cannot contain static members.

A local class has approximately the same relationship to a member class that a local variable in a method has to an instance variable of the classcontaining the method.

What's next?

The next module in this series will explain and discuss anonymous classes, and will also compare anonymous classes to local classes.

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Java OOP: Local Classes
  • File: Java1638.htm
  • Published: 11/19/13

Questions & Answers

calculate molarity of NaOH solution when 25.0ml of NaOH titrated with 27.2ml of 0.2m H2SO4
Gasin Reply
what's Thermochemistry
rhoda Reply
the study of the heat energy which is associated with chemical reactions
Kaddija
How was CH4 and o2 was able to produce (Co2)and (H2o
Edafe Reply
explain please
Victory
First twenty elements with their valences
Martine Reply
what is chemistry
asue Reply
what is atom
asue
what is the best way to define periodic table for jamb
Damilola Reply
what is the change of matter from one state to another
Elijah Reply
what is isolation of organic compounds
IKyernum Reply
what is atomic radius
ThankGod Reply
Read Chapter 6, section 5
Dr
Read Chapter 6, section 5
Kareem
Atomic radius is the radius of the atom and is also called the orbital radius
Kareem
atomic radius is the distance between the nucleus of an atom and its valence shell
Amos
Read Chapter 6, section 5
paulino
Bohr's model of the theory atom
Ayom Reply
is there a question?
Dr
when a gas is compressed why it becomes hot?
ATOMIC
It has no oxygen then
Goldyei
read the chapter on thermochemistry...the sections on "PV" work and the First Law of Thermodynamics should help..
Dr
Which element react with water
Mukthar Reply
Mgo
Ibeh
an increase in the pressure of a gas results in the decrease of its
Valentina Reply
definition of the periodic table
Cosmos Reply
What is the lkenes
Da Reply
what were atoms composed of?
Moses Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask