总是报错说"无法从静态上下文中引用非静态变量this"

cheng19840218 2008-04-15 09:48:39
总是报错说"无法从静态上下文中引用非静态变量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;
}
}
...全文
3732 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
ifuleyou12345 2010-09-02
  • 打赏
  • 举报
回复
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
l362919367 2010-06-20
  • 打赏
  • 举报
回复
return 0 后面少个大括号
valrent 2009-02-16
  • 打赏
  • 举报
回复
可以认为静态方法是没有this参数的方法(在一个非静态的方法中,this参数表示该方法的隐式参数)。出现那个错误提示,肯定是你在静态上下文中调用了非静态的东东,要是用了内部类,可以new一个外围类的对象,来调用内部类。
mengweilil 2009-02-16
  • 打赏
  • 举报
回复
内部类static,就不能引用定义它的外部类的参数了
狂浪吻血 2009-02-16
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 tang3dashu 的回复:]
引用 7 楼 lastsweetop 的回复:
楼主的意思是想用内部类
但是main函数是static方法
而内部类是非静态方法,所以导致出错
将内部类改成static就可以了


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

强烈建议你内部类别static
vnmw2000 2009-02-15
  • 打赏
  • 举报
回复
这位朋友 你应该是由于写了内部类而导致的这种 结果 ,外部类隐式的调用了this 而你的内部类是非静态的
导致你的代码 从 静态上下文中引用了非静态变量 this
yangk_105 2008-04-15
  • 打赏
  • 举报
回复
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
吴冬冬 2008-04-15
  • 打赏
  • 举报
回复
内部类隐含的含有this关键字
但是我并没有看到类似楼主的错误
吴冬冬 2008-04-15
  • 打赏
  • 举报
回复
楼主的意思是想用内部类
但是main函数是static方法
而内部类是非静态方法,所以导致出错
将内部类改成static就可以了
whmjxa 2008-04-15
  • 打赏
  • 举报
回复
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;
}
}
吴冬冬 2008-04-15
  • 打赏
  • 举报
回复
代码没有任何错误,应该是你括号不对称造成的
调整过括号后的代码

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;
}
}
kingssq 2008-04-15
  • 打赏
  • 举报
回复
类中定义类是没有问题,但你这里的错误好像与你描述的不符合,我找了好久都没有找到你在哪里用了this???
kevinchj 2008-04-15
  • 打赏
  • 举报
回复

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;
}
}


其他逻辑没看!
kevinchj 2008-04-15
  • 打赏
  • 举报
回复
怎么能在一个类中又定义一个类呢?检查大括号{}
kevinchj 2008-04-15
  • 打赏
  • 举报
回复
很多错误!
tang3dashu 2008-04-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lastsweetop 的回复:]
楼主的意思是想用内部类
但是main函数是static方法
而内部类是非静态方法,所以导致出错
将内部类改成static就可以了
[/Quote]

同意 预见过,在main方法里边调用静态的方法函数时候 定义必须也是static
virusswb 2008-04-15
  • 打赏
  • 举报
回复
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方法也放错地方了,是静态类的方法,就放在类内部
virusswb 2008-04-15
  • 打赏
  • 举报
回复
内部类是可以的,但是内部类的this指针会失效,所以
public String toString() {
return "Year:Month:Day--" + year + "-" + month + "-" + day;
}
}
里面的变量他会找不到,没有了this指针了
junny9985 2008-04-15
  • 打赏
  • 举报
回复
类中定义类 是可以的 叫做:内部类。一般不推荐用。不符合面向对象的思想

在静态方法中,不可以调用非静态的变量 我给你讲下原来,

在虚拟机工作的时候,会自动加载static修饰的方法和变量,他们在加载的时候和类是没有关系的,

一般非静态的属性和方法必须要实例化类才能访问到,对吧?如果静态里包含非静态的,那么在第一次加载的时候,你让虚拟机怎

么做呢?是先实例化一个类,然后在加载静态的?

不符合逻辑吗?对吧

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧