<< 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

Application of nanotechnology in medicine
what is variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
Damian
yes that's correct
Professor
I think
Professor
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
Rafiq
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
Damian
How we are making nano material?
LITNING Reply
what is a peer
LITNING Reply
What is meant by 'nano scale'?
LITNING Reply
What is STMs full form?
LITNING
scanning tunneling microscope
Sahil
how nano science is used for hydrophobicity
Santosh
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
Rafiq
what is differents between GO and RGO?
Mahi
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
Rafiq
if virus is killing to make ARTIFICIAL DNA OF GRAPHENE FOR KILLED THE VIRUS .THIS IS OUR ASSUMPTION
Anam
analytical skills graphene is prepared to kill any type viruses .
Anam
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
Bob
The nanotechnology is as new science, to scale nanometric
brayan
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Damian
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
Adin
why?
Adin
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
Praveena Reply
hi
Loga
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
Berger describes sociologists as concerned with
Mueller Reply
what is hormones?
Wellington
Got questions? Join the online conversation and get instant answers!
Jobilize.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?

Ask