Java Java: Check LinkedList<T> Classes

  • Thread starter Thread starter Live4eva_2
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
The discussion centers on a Java code issue regarding class visibility and accessibility within nested classes. The user is encountering a compilation error stating that "Node<T> current" is not accessible in the LinkedList<T> class. The problem arises because the current member of the Iterator<T> class is declared as protected, meaning it can only be accessed by the Iterator class and its subclasses, but not by the outer LinkedList class. This limitation on access is clarified, emphasizing that the protected modifier restricts visibility to the same class and its subclasses, not to the enclosing class. The user also questions whether only one inner class is permitted, but this is not the case; multiple inner classes can exist within an outer class. Overall, the key takeaway is the importance of understanding access modifiers in Java, particularly when dealing with nested classes.
Live4eva_2
Messages
28
Reaction score
0
Could someone check the following 3 classes for me please?

Java:
public class LinkedList<T>
{
    public Node<T> first;
    public Node<T> last;
    protected class Node<T>
    {
        public Node<T> next;
        public Node<T> last;

    }
    public class Iterator<T>
    {
        protected Node<T> current;
        protected Node<T> previous;

    }

}
I've only included the classes and variables in order to illustrate the visibility.
when I complile I'm getting the msg that "Node<T> current" isn't available in LinkedList<T> class...why not !?Am I only allowed one inner class?Also i ensured that my iterator was constructed within the LinkedList constructor so i don't think that it could be a problem with instantiating the iterator object.
 
Last edited by a moderator:
Technology news on Phys.org
The current member of Iterator<T> is protected, so it is accessible only to other members of that class or subclasses of that class. It is not accessible to outer scopes, which would include the LinkedList class.

I'm reasonably sure about what I've said, but I haven't really written any Java code for many years. If anybody has an opposing view, please jump in.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top