java resolves two schemes for the instance of type Printlistfromtailtohead is accessible of no enclosing _java

Source: Internet
Author: User
Tags static class

The following problems were encountered today when compiling Java programs:

No enclosing instance of type Printlistfromtailtohead is accessible. Must qualify the allocation with a enclosing instance
of type printlistfromtailtohead (e.g. X.new A () where x is A instance of Printlistfromtailtohead).

The source code is:

public class Printlistfromtailtohead {public
static void Main (string[] args) {
ListNode one = new ListNode (1); 
   listnode two = new ListNode (2);
ListNode three = new ListNode (3);
One.next = two;
Two.next = three;
arraylist<integer> result = Printlistfromtailtohead (one);
SYSTEM.OUT.PRINTLN ("Results are:" + result);
}
Class ListNode {public
int val;
Public ListNode next;
Public ListNode () {
} public
listnode (int val) {
this.val = val;
}
}
public static arraylist<integer> Printlistfromtailtohead (ListNode listnode) {
stack<integer> Stack = New Stack<integer> ();
while (ListNode!= null) {
stack.push (listnode.val);
ListNode = Listnode.next;
}
arraylist<integer> ArrayList = new arraylist<integer> ();
while (!stack.isempty ()) {
Arraylist.add (Stack.pop ());
}
Return arrayList
} 
}

Question Explanation:

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 like a static method cannot invoke a dynamic method.

There are two ways to solve this problem:

First type:

Defines the inner class ListNode as a statically static class.

The second type:

The inner class ListNode are defined outside the Printlistfromtailtohead class.

Two ways to solve this problem:

First type:

public class Printlistfromtailtohead {public
static void Main (string[] args) {
ListNode one = new ListNode (1); 
   listnode two = new ListNode (2);
ListNode three = new ListNode (3);
One.next = two;
Two.next = three;
arraylist<integer> result = Printlistfromtailtohead (one);
SYSTEM.OUT.PRINTLN ("Results are:" + result);
}
Static class ListNode {public
int val;
Public ListNode next;
Public ListNode () {
} public
listnode (int val) {
this.val = val;
}
}

The second type:

public class Printlistfromtailtohead {public
static void Main (string[] args) {
ListNode one = new ListNode (1); 
   listnode two = new ListNode (2);
ListNode three = new ListNode (3);
One.next = two;
Two.next = three;
}
public static arraylist<integer> Printlistfromtailtohead (ListNode listnode) {
stack<integer> Stack = New Stack<integer> ();
while (ListNode!= null) {
stack.push (listnode.val);
ListNode = Listnode.next;
}
arraylist<integer> ArrayList = new arraylist<integer> ();
while (!stack.isempty ()) {
Arraylist.add (Stack.pop ());
}
Return arrayList
}
}
Class ListNode {public
int val;
Public ListNode next;
Public ListNode () {
} public
listnode (int val) {
this.val = val;
}
}

The above is a small set to introduce the Java solution No enclosing instance of type Printlistfromtailtohead is accessible the problem of two scenarios, I hope to help.

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.