是否可以在main中调用非静态方法?

时间:2015-01-17 19:43:52

标签: java

我试图将链接列表所代表的两个数字相加。我有静态说明符和内部类的问题。我定义了一个内部类SumWrapper来跟踪进位。我在sum方法中使用了这个包装器。我首先将它声明为静态方法,但我在Eclipse上遇到以下错误:

  

无法访问类型为Solution的封闭实例。必须符合资格   使用类型为Solution的封闭实例进行分配(例如x.new A()   其中x是Exo25V2的一个实例。)

然后我删除了静态说明符,但它无法从main ...

调用我的方法

有解决方法吗?

public class Solution {

  public class SumWrapper{
    public int carry = 0;
    public Node node;

    public SumWrapper(int c, Node n){
      carry = c;
      node = n;
    }
  } // close sumWrapper class

  public SumWrapper sum(Node node1, Node node2){
    if (node1 == null && node2 == null){
      SumWrapper result = new SumWrapper(0, null);
      return result;
    }

    int current = node1.data + node2.data + sum(node1.next, node2.next).carry;
    int carry = (current >= 10) ? 1 : 0;
    current = current % 10;

    Node sumResult = new Node(current);
    sumResult.next = sum(node1.next, node2.next).node;

    SumWrapper result = new SumWrapper(carry, sumResult);

    return result;
  } // close sumWrapper method

  public int listSize(Node node){
    int result = 0;

    while (node != null){
      result++;
      node = node.next;
    }
    return result;
  } // close listSize method

  public Node sumLists(Node node1, Node node2){
    int size1 = listSize(node1);
    int size2 = listSize(node2);
    int size = (size1 >= size2) ? (size1+1):(size2+1);

    while (size1 < size){
      Node head1 = new Node(0);
      head1.next = node1;
      node1 = head1;
      size1++;
    }

    while (size2 < size){
      Node head2 = new Node(0);
      head2.next = node2;
      node2 = head2;
      size2++;
    }

    SumWrapper wrap = sum(node1, node2);

    return wrap.node;

  } // close sumLists method

  public static void main(String[] args){
    Node head1 = new Node(6);
    head1.appendToTail(1);
    head1.appendToTail(1);
    head1.appendToTail(7);

    Node head2 = new Node(2);
    head2.appendToTail(9);
    head2.appendToTail(5);

    Node result = sumLists(head1, head2);
    Node.printLinkedList(result);

  } // close main method
}

8 个答案:

答案 0 :(得分:5)

问题是您正在从sumLists方法调用非静态方法(main)。 main方法是静态的,因此只能访问同一类中的其他静态方法。

除非......你创建一个例如Solution

public static void main(String... args) {
    Solution s = new Solution();
    s.sumLists(...); // now you invoke it using an instance method (i.e. a non-static method)
}

所以,你的选择是:

  1. 让您的方法保持静态。
  2. 创建类的实例并调用非静态方法。
  3. 有关这方面的一些好的阅读,请查看Oracle tutorial

答案 1 :(得分:1)

简而言之,您不能从静态方法中调用任何非静态方法。静态方法属于非静态方法属于对象的类。

您需要将SumWrapper声明为静态以及从main调用的所有方法。

答案 2 :(得分:1)

除main()方法外,您可以将整个代码移动到另一个类。然后在main()中创建类的对象并访问方法sumLists。

答案 3 :(得分:1)

您的问题是SumWrapper课程不是static。必须从外部类的实例初始化非静态内部类,以便它们可以访问该实例。这就是你收到的错误所在。

但是你的SumWrapper课程并没有使用它的父母&#34; Solution对象,所以只需将其设为静态:

  public static class SumWrapper

答案 4 :(得分:1)

为什么不将SumWrapper类声明为静态?

它似乎不需要访问封闭实例的任何状态。

答案 5 :(得分:0)

当你正在创建一个其他对象的实例并从这个类中调用一些方法

时,它是允许的

答案 6 :(得分:0)

您必须实例化Solution,然后从结果对象中调用sumLists或将sumLists和listSize声明为static。

答案 7 :(得分:0)

这是我解决问题的一般方案。

if T1.AcctCode like 61

}

   public class Customer {
public static void main(String[] args) {
Customer customer=new Customer();
customer.Business();

} }