总是报错说"无法从静态上下文中引用非静态变量this",我快崩溃了,求各位大哥达人指点一下public class TestDateSort {
public static void main(String[] args) {
Date[] days = new Date[5];
days[0] = new Date(2006,5,4);
days[1] = new Date(2006,7,4);
days[2] = new Date(2008,5,4);
days[3] = new Date(2004,5,9);
days[4] = new Date(2004,5,4); bubbleSort(days); for(int i=0; i<days.length; i++){
System.out.println(days[i]);
}
} public static Date[] bubbleSort(Date[] a) {
int len = a.length;
for(int i = len-1;i>=1;i--) {
for(int j=0 ; j<=i-1 ; j++) {
if(a[j].compare(a[j+1]) > 0) {
Date temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
return a;
} class Date {
int year, month, day; Date(int y,int m,int d) {
year = y;
month = m;
day = d;
} public int compare(Date date) {
return year>date.year?1
:year<date.year?-1
:month>date.month?1
:month<date.month?-1
:day>date.day?1
:day<date.day?-1
:0;
}
} public String toString() {
return "Year:Month:Day--" + year + "-" + month + "-" + day;
}
}

解决方案 »

  1.   


    public class TestDateSort {
    public static void main(String[] args) {
    Date[] days = new Date[5];
    days[0] = new Date(2006, 5, 4);
    days[1] = new Date(2006, 7, 4);
    days[2] = new Date(2008, 5, 4);
    days[3] = new Date(2004, 5, 9);
    days[4] = new Date(2004, 5, 4); bubbleSort(days); for (int i = 0; i < days.length; i++) {
    System.out.println(days[i]);
    }
    } public static Date[] bubbleSort(Date[] a) {
    int len = a.length;
    for (int i = len - 1; i >= 1; i--) {
    for (int j = 0; j <= i - 1; j++) {
    if (a[j].compare(a[j + 1]) > 0) {
    Date temp = a[j];
    a[j] = a[j + 1];
    a[j + 1] = temp;
    }
    }
    }
    return a;
    }
    }class Date {
    int year, month, day; Date(int y, int m, int d) {
    year = y;
    month = m;
    day = d;
    } public int compare(Date date) {
    return year > date.year ? 1 : year < date.year ? -1
    : month > date.month ? 1 : month < date.month ? -1
    : day > date.day ? 1 : day < date.day ? -1 : 0;
    } public String toString() {
    return "Year:Month:Day--" + year + "-" + month + "-" + day;
    }
    }
    其他逻辑没看!
      

  2.   

    类中定义类是没有问题,但你这里的错误好像与你描述的不符合,我找了好久都没有找到你在哪里用了this???
      

  3.   

    代码没有任何错误,应该是你括号不对称造成的
    调整过括号后的代码public class TestDateSort {
    public static void main(String[] args) {
    Date[] days = new Date[5];
    days[0] = new Date(2006, 5, 4);
    days[1] = new Date(2006, 7, 4);
    days[2] = new Date(2008, 5, 4);
    days[3] = new Date(2004, 5, 9);
    days[4] = new Date(2004, 5, 4); bubbleSort(days); for (int i = 0; i < days.length; i++) {
    System.out.println(days[i]);
    }
    } public static Date[] bubbleSort(Date[] a) {
    int len = a.length;
    for (int i = len - 1; i >= 1; i--) {
    for (int j = 0; j <= i - 1; j++) {
    if (a[j].compare(a[j + 1]) > 0) {
    Date temp = a[j];
    a[j] = a[j + 1];
    a[j + 1] = temp;
    }
    }
    }
    return a;
    }
    }class Date {
    int year, month, day; Date(int y, int m, int d) {
    year = y;
    month = m;
    day = d;
    } public int compare(Date date) {
    return year > date.year ? 1 : year < date.year ? -1
    : month > date.month ? 1 : month < date.month ? -1
    : day > date.day ? 1 : day < date.day ? -1 : 0;
    } public String toString() {
    return "Year:Month:Day--" + year + "-" + month + "-" + day;
    }
    }
      

  4.   

    public int compare(Date date) { 
    return year>date.year?1 
    :year <date.year?-1 
    :month>date.month?1 
    :month <date.month?-1 
    :day>date.day?1 
    :day <date.day?-1 
    :0; 

    } //这个}是多余的,去掉public String toString() { 
    return "Year:Month:Day--" + year + "-" + month + "-" + day; 


      

  5.   

    楼主的意思是想用内部类
    但是main函数是static方法
    而内部类是非静态方法,所以导致出错
    将内部类改成static就可以了
      

  6.   

    内部类隐含的含有this关键字
    但是我并没有看到类似楼主的错误
      

  7.   

    public class TestDateSort {
    public static void main(String[] args) {
    TestDateSort t = new TestDateSort();
    Date[] days = new Date[5];
    days[0] = t.new Date(2006, 5, 4);
    days[1] = t.new Date(2006, 7, 4);
    days[2] = t.new Date(2008, 5, 4);
    days[3] = t.new Date(2004, 5, 9);
    days[4] = t.new Date(2004, 5, 4); bubbleSort(days); for (int i = 0; i < days.length; i++) {
    System.out.println(days[i]);
    }
    } public static Date[] bubbleSort(Date[] a) {
    int len = a.length;
    for (int i = len - 1; i >= 1; i--) {
    for (int j = 0; j <= i - 1; j++) {
    if (a[j].compare(a[j + 1]) > 0) {
    Date temp = a[j];
    a[j] = a[j + 1];
    a[j + 1] = temp;
    }
    }
    }
    return a;
    } class Date {
    int year, month, day; Date(int y, int m, int d) {
    year = y;
    month = m;
    day = d;
    } public int compare(Date date) {
    return year > date.year ? 1 : year < date.year ? -1
    : month > date.month ? 1 : month < date.month ? -1
    : day > date.day ? 1 : day < date.day ? -1 : 0;
    }
    } public String toString(Date date) {
    return "Year:Month:Day--" +date.year + "-" + date.month + "-" + date.day;
    }
    }帮你改了一下,因为你把class Date 写在了class TestDateSort 的内部,所以,前者就相当于后者的一个非静态的类变量,非静态的变量当然不可以直接放到静态的main函数中使用,必须通过class TestDateSort的对象实例来调用另外,你也可以像楼上说的直接将class Date放到class TestDateSort 的外面还可以直接在class Date前面加上static
      

  8.   

    类中定义类 是可以的 叫做:内部类。一般不推荐用。不符合面向对象的思想在静态方法中,不可以调用非静态的变量 我给你讲下原来,在虚拟机工作的时候,会自动加载static修饰的方法和变量,他们在加载的时候和类是没有关系的,一般非静态的属性和方法必须要实例化类才能访问到,对吧?如果静态里包含非静态的,那么在第一次加载的时候,你让虚拟机怎么做呢?是先实例化一个类,然后在加载静态的?不符合逻辑吗?对吧
      

  9.   

    内部类是可以的,但是内部类的this指针会失效,所以
    public String toString() { 
    return "Year:Month:Day--" + year + "-" + month + "-" + day; 

    }
    里面的变量他会找不到,没有了this指针了
      

  10.   

    package com.virus;import java.util.Date;public class Overloading {
    static class Date {
    int year, month, day; Date(int y, int m, int d) {
    year = y;
    month = m;
    day = d;
    } public int compare(Date date) {
    return year > date.year ? 1 : year < date.year ? -1
    : month > date.month ? 1 : month < date.month ? -1
    : day > date.day ? 1 : day < date.day ? -1 : 0;
    } public String toString() {
    return "Year:Month:Day--" + year + "-" + month + "-" + day;
    }
    } public static void main(String[] args) {
    Date[] days = new Date[5];
    days[0] = new Date(2006, 5, 4);
    days[1] = new Date(2006, 7, 4);
    days[2] = new Date(2008, 5, 4);
    days[3] = new Date(2004, 5, 9);
    days[4] = new Date(2004, 5, 4); bubbleSort(days); for (int i = 0; i < days.length; i++) {
    System.out.println(days[i]);
    }
    } public static Date[] bubbleSort(Date[] a) {
    int len = a.length;
    for (int i = len - 1; i >= 1; i--) {
    for (int j = 0; j <= i - 1; j++) {
    if (a[j].compare(a[j + 1]) > 0) {
    Date temp = a[j];
    a[j] = a[j + 1];
    a[j + 1] = temp;
    }
    }
    }
    return a;
    }}
    要用静态类,而且tostring方法也放错地方了,是静态类的方法,就放在类内部
      

  11.   


    同意 预见过,在main方法里边调用静态的方法函数时候 定义必须也是static
      

  12.   

    这位朋友  你应该是由于写了内部类而导致的这种 结果   ,外部类隐式的调用了this 而你的内部类是非静态的
    导致你的代码 从 静态上下文中引用了非静态变量 this
      

  13.   

    可以认为静态方法是没有this参数的方法(在一个非静态的方法中,this参数表示该方法的隐式参数)。出现那个错误提示,肯定是你在静态上下文中调用了非静态的东东,要是用了内部类,可以new一个外围类的对象,来调用内部类。
      

  14.   


    强烈建议你内部类别static
      

  15.   

    内部类static,就不能引用定义它的外部类的参数了
      

  16.   

    return 0 后面少个大括号
      

  17.   

    when you see the compiled inner class , you will find JVM add some code before (or after) constructor to set a final reference (called this$0 if it is 1 level nested) field for Outer Instance and it is implemented by Constructor's Primary expression. So if you want to initiate a instance of an inner class , you will have to set the Primary expression to pass a Outer instance , such as (new Outer()).new Inner(), (the new Outer()) is the primary expression