<< Chapter < Page Chapter >> Page >

Accessing the object of the class C, and the this keyword

The syntax used with the keyword this is somewhat different for member classes and contained objects than is the case fortop-level classes. For example, continuing with the method named cShow , the code in Listing 17 shows five different ways to access the object instantiated from the member class named C in order to get and display thename of the class file that represents the member class named C.

The screen output

All five statements in Listing 17 display the name of the same class file, as shown in Figure 10 .

Obviously in this situation, the last three statements in Listing 17 are overly complex. There is no particular problem writing code in the methodnamed cShow to gain access to the object to which the method belongs. It isn't even necessary to use this to refer to that object, although the use of the hidden reference this may make the code more readable.

Accessing the containing object of the class B

However, things get a little more complicated when you need to gain access to acontaining object, such as the containing object instantiated from the class named B.

The two statements shown in Listing 18 gain access to the containing object of the class named B. Each statement gets and displays thename of the class file that represents the member class named B. (Note the use of the keyword this in these statements.)

The screen output

The output produced by the code in Listing 18 is shown in Figure 11 . Once again, both statements get and display the name of the same class file.

Accessing the containing object of the class named A

Finally, the code in Listing 19 gains access to the containing object of the class named A. (Once again, note the use of the this keyword in the statement in Listing 19 .)

The code in Listing 19 produces the output shown in Figure 12 .

(Since the class named A is a top-level class, the name of the class file is the same as the name of the class, with no $ characters inserted by thecompiler.)

Investigate independent objects of classes A and B

Recall that when the object of the member class named C was instantiated, theconstructor for the class instantiated independent objects of the enclosing classes named A and B, and saved those object's references in instancevariables of the class named C.

(See Listing 11 , noting the parameter values of 10 and 20 passed to the constructors for A and B. Recall that the constructors for A and B savethose parameter values in private instance variables named aVar and bVar .)

Display variable values and class file names

The code in Listing 20 displays the values stored in the private instance variables belonging to thoseobjects. The code in Listing 20 also displays the names of the class files representing the classes from which those objects were instantiated.

Screen output

The code in Listing 20 produces the output shown in Figure 13 .

There should be no surprises in the output shown in Figure 13 . The values of the instance variables match the parameter values passed to theconstructors in Listing 11 when the objects were instantiated. The class file names match what you already know to be true from previous discussionsearlier in this module.

Questions & Answers

what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
Do somebody tell me a best nano engineering book for beginners?
s. Reply
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
types of nano material
abeetha Reply
I start with an easy one. carbon nanotubes woven into a long filament like a string
many many of nanotubes
what is the k.e before it land
what is the function of carbon nanotubes?
I'm interested in nanotube
what is nanomaterials​ and their applications of sensors.
Ramkumar Reply
what is nano technology
Sravani Reply
what is system testing?
preparation of nanomaterial
Victor Reply
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Berger describes sociologists as concerned with
Mueller Reply
Got questions? Join the online conversation and get instant answers!
QuizOver.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?