Java solves no enclosing instance of type Printlistfromtailtohead is accessible problem

Source: Internet
Author: User
Tags static class

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.