<< Chapter < Page Chapter >> Page >
In Java, besides having fields and methods, a class can also have other classes as members. Just like fields and methods, a class member of can be static or non-static. A non-static class member is called an inner class. Inner class is a programming construct based on the powerful concept of closure prevalent in the functional programming paradigm. It allows on-the-fly creation of objects, which can communicate transparently with the enclosing object inside of which they come into existence. We illustrate the rationale and use of inner classes via a progression from top level helper classes to named static nested classes, to non-static named classes, and to anonymous inner classes with dynamic on-the-fly instantiation.

1. helpers are the variants

Consider again the familiar problem of computing a String representation of an IList that displays a comma-separated list of its elements delimited by a pair of matching parentheses. We have seen at least one way to compute such a String representation using a visitor called ToStringAlgo . Below are three different algorithms, ToString1 , ToString2 and ToString3 , that compute the same String representation.

Main Visitor ToString1 Tail-recursive helper ToString1Help
package listFW.visitor; import listFW.*;public class ToString1 implements IListAlgo {public static final ToString1 Singleton = new ToString1();private ToString1() {} public Object emptyCase(IMTList host, Object... nu) { return "()";}public Object nonEmptyCase( INEList host, Object... nu) {return host.getRest().execute( ToString1Help.Singleton,"(" + host.getFirst()); }} /** * Helps ToString1 compute the String* representation of the rest of the list. * It takes as input the accumulated* string representation of the preceding * list. This accumulated string contains* the left most parenthesis and all the * elements of the preceding list, each* separated by a comma. */class ToString1Help implements IListAlgo { public static final ToString1HelpSingleton = new ToString1Help(); private ToString1Help() {}public Object emptyCase( IMTList host, Object... acc) {return acc[0] + ")";}public Object nonEmptyCase( INEList host, Object... acc) {return host.getRest().execute(this, acc[0]+ ", " + host.getFirst());} }
Main Visitor ToString2 Tail-recursive helper ToString2Help
package listFW.visitor; import listFW.*;public class ToString2 implements IListAlgo {public static final ToString2 Singleton = new ToString2();private ToString2() {}public Object emptyCase(IMTList host, Object... nu) {return "()"; }public Object nonEmptyCase(INEList host, Object... nu) {return host.getRest().execute( ToString2Help.Singleton,host.getFirst().toString()); }} /** * Helps ToString2 compute the Stringrepresentation of the rest of the list. */class ToString2Help implements IListAlgo { public static final ToString2HelpSingleton = new ToString2Help(); private ToString2Help() {}public Object emptyCase( IMTList host, Object... acc) {return "(" + acc[0]+ ")"; }public Object nonEmptyCase( INEList host, Object... acc) {return host.getRest().execute(this, acc[0]+ ", " + host.getFirst());} }

Questions & Answers

what is defense mechanism
Chinaza Reply
what is defense mechanisms
Chinaza
I'm interested in biological psychology and cognitive psychology
Tanya Reply
what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai 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, Principles of object-oriented programming. OpenStax CNX. May 10, 2013 Download for free at http://legacy.cnx.org/content/col10213/1.37
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?

Ask