The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
SeeDev.java for updated tutorials taking advantage of the latest releases.
SeeJava Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
SeeJDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
Question: The programProblem.java doesn't compile. What do you need to do to make it compile? Why?
Answer: Deletestatic in front of the declaration of theInner class. An static inner class does not have access to the instance fields of the outer class. SeeProblemSolved.java.
Use the Java API documentation for theBox class (in thejavax.swing package) to help you answer the following questions.
Question: What static nested class doesBox define?
Answer:Box.Filler
Question: What inner class doesBox define?
Answer:Box.AccessibleBox
Question: What is the superclass ofBox's inner class?
Answer:[java.awt.]Container.AccessibleAWTContainer
Question: Which ofBox's nested classes can you use from any class?
Answer:Box.Filler
Question: How do you create an instance ofBox'sFiller class?
Answer:new Box.Filler(minDimension, prefDimension, maxDimension)
Exercise: Get the fileClass1.java. Compile and runClass1. What is the output?
Answer:InnerClass1: getString invoked.
InnerClass1: getAnotherString invoked.
Exercise: The following exercises involve modifying the classDataStructure.java, which the sectionInner Class Example discusses.
Define a method namedprint(DataStructureIterator iterator). Invoke this method with an instance of the classEvenIterator so that it performs the same function as the methodprintEven.
Hint: These statements do not compile if you specify them in themain method:
DataStructure ds = new DataStructure();ds.print(new EvenIterator());
The compiler generates the error message, "non-static variable this cannot be referenced from a static context" when it encounters the expressionnew EvenIterator(). The classEvenIterator is an inner, non-static class. This means that you can create an instance ofEvenIterator only inside an instance of the outer class,DataStructure.
You can define a method inDataStructure that creates and returns a new instance ofEvenIterator.
Invoke the methodprint(DataStructureIterator iterator) so that it prints elements that have an odd index value. Use an anonymous class as the method's argument instead of an instance of the interfaceDataStructureIterator.
Hint: You cannot access the private membersSIZE andarrayOfInts outside the classDataStructure, which means that you cannot access these private members from an anonymous class defined outsideDataStructure.
You can define methods that access the private membersSIZE andarrayOfInts and then use them in your anonymous class.
Define a method namedprint(java.util.Function<Integer, Boolean> iterator) that performs the same function asprint(DataStructureIterator iterator). Invoke this method with a lambda expression to print elements that have an even index value. Invoke this method again with a lambda expression to print elements that have an odd index value.
Hint: In thisprint method, you can step though the elements contained in the arrayarrayOfInts with afor expression. For each index value, invoke the methodfunction.apply. If this method returns a true value for a particular index value, print the element contained in that index value.
To invoke thisprint method to print elements that have an even index value, you can specify a lambda expression that implements the methodBoolean Function.apply(Integer t). This lambda expression takes oneInteger argument (the index) and returns aBoolean value (Boolean.TRUE if the index value is even,Boolean.FALSE otherwise).
Define two methods so that these statements print elements that have an even index value and then elements that have an odd index value:
DataStructure ds = new DataStructure()// ...ds.print(DataStructure::isEvenIndex);ds.print(DataStructure::isOddIndex);
Hint: Create two methods in the classDataStructure namedisEvenIndex andisOddIndex that have the same parameter list and return type as the abstract methodBoolean Function<Integer, Boolean>.apply(Integer t). This means that the methods take oneInteger argument (the index) and return aBoolean value.
Answer: See the fileDataStructure.java.
About Oracle |Contact Us |Legal Notices |Terms of Use |Your Privacy Rights
Copyright © 1995, 2024 Oracle and/or its affiliates. All rights reserved.