总是报错说"无法从静态上下文中引用非静态变量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;
}
}
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;
}
}
解决方案 »
- 自定义classLoad发生NoClassDefFoundError错误
- 关于set path的问题
- 设计思路,求助
- 线程池中的线程可以多次使用吗?
- 讨论关于Vector与ArrayList
- 怎么设置ACTION的快捷键?就是CTRL+“字母”的?
- 高分,在线等:为何updatedouble、updatefloat总不成功,而用updateint就可以?
- 我的担心!
- swing applet用htmlconverter如何转化?我的jdk1.3中找不到htmlconverter这个东东。
- 帮我看看这段代码,很奇怪啊
- 请教:struts2 如何实现连接池的管理和使用?
- 在dos中输入法切换不了是怎么回事呀,
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;
}
}
其他逻辑没看!
调整过括号后的代码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;
}
}
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;
}
}
但是main函数是static方法
而内部类是非静态方法,所以导致出错
将内部类改成static就可以了
但是我并没有看到类似楼主的错误
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
public String toString() {
return "Year:Month:Day--" + year + "-" + month + "-" + day;
}
}
里面的变量他会找不到,没有了this指针了
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方法也放错地方了,是静态类的方法,就放在类内部
同意 预见过,在main方法里边调用静态的方法函数时候 定义必须也是static
导致你的代码 从 静态上下文中引用了非静态变量 this
强烈建议你内部类别static