The following problems were encountered in compiling Java programs today:
No enclosing instance of type Printlistfromtailtohead is accessible. Must qualify the allocation with an enclosing instance
of type printlistfromtailtohead (e.g. X.new A () where x is an instance of Printlistfromtailtohead).
The source code is:
1 Public classPrintlistfromtailtohead {2 3 Public Static voidMain (string[] args) {4ListNode one =NewListNode (1);5ListNodeNewListNode (2);6ListNode three =NewListNode (3);7One.next =both ;8Two.next =three;9 Tenarraylist<integer> result =Printlistfromtailtohead (one); One ASYSTEM.OUT.PRINTLN ("The result is:" +result); - } - the classListNode { - - Public intVal; - PublicListNode Next; + - PublicListNode () { + A } at - PublicListNode (intval) { - This. val =Val; - } - } - in Public StaticArraylist<integer>Printlistfromtailtohead (ListNode listnode) { - tostack<integer> stack =NewStack<integer>(); + while(ListNode! =NULL) { - Stack.push (listnode.val); theListNode =Listnode.next; * } $ Panax NotoginsengArraylist<integer> ArrayList =NewArraylist<integer>(); - while(!Stack.isempty ()) { the Arraylist.add (Stack.pop ()); + } A returnarrayList; the } $}
Explanation of the problem:
In the code, my ListNode class is the inner class defined in the Printlistfromtailtohead class. The listnode inner class is a dynamic inner class, and my main method is static.
Just as a static method cannot invoke a dynamic method.
There are two ways to solve this problem:
The first type:
Defines the inner class ListNode as a statically static class.
The second type:
The inner class ListNode is defined outside the Printlistfromtailtohead class.
Two workarounds:
The first type:
1 Public classPrintlistfromtailtohead {2 3 Public Static voidMain (string[] args) {4ListNode one =NewListNode (1);5ListNodeNewListNode (2);6ListNode three =NewListNode (3);7One.next =both ;8Two.next =three;9 Tenarraylist<integer> result =Printlistfromtailtohead (one); One ASYSTEM.OUT.PRINTLN ("The result is:" +result); - } - the Static classListNode { - - Public intVal; - PublicListNode Next; + - PublicListNode () { + A } at - PublicListNode (intval) { - This. val =Val; - } -}
The second type:
1 Public classPrintlistfromtailtohead {2 3 Public Static voidMain (string[] args) {4ListNode one =NewListNode (1);5ListNodeNewListNode (2);6ListNode three =NewListNode (3);7One.next =both ;8Two.next =three;9 }Ten One Public StaticArraylist<integer>Printlistfromtailtohead (ListNode listnode) { A -stack<integer> stack =NewStack<integer>(); - while(ListNode! =NULL) { the Stack.push (listnode.val); -ListNode =Listnode.next; - } - +Arraylist<integer> ArrayList =NewArraylist<integer>(); - while(!Stack.isempty ()) { + Arraylist.add (Stack.pop ()); A } at returnarrayList; - } - } - - classListNode { - in Public intVal; - PublicListNode Next; to + PublicListNode () { - the } * $ PublicListNode (intval) {Panax Notoginseng This. val =Val; - } the}
Java solves no enclosing instance of type Printlistfromtailtohead is accessible problem