为了账号安全,请及时绑定邮箱和手机立即绑定

求指点,实在是找不到错误在哪了!!!

求指点,实在是找不到错误在哪了!!!

慕运维8079593 2023-04-14 19:15:19
package New;public class Person { public String name; public String sex; public int age; public Person(String a,String b,int c){ name=a; sex=b; age=c; } Person tom=new Person("tom","男",12); public static void main(String[] args){ System.out.println(tom.sex);//Problems:cant make a static reference to the non-static field p1 } }
查看完整描述

2 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

sex是一个非静态变量,在一个静态函数里不能调用非静态变量。因为非静态变量是存活在实例中的,而静态还是则不是,即使没有实例也能被调用。

查看完整回答
反对 回复 2023-04-17
?
皈依舞

TA贡献1851条经验 获得超3个赞

静态方法里是不能调用非静态成员的

Person tom=new Person("tom","男",12);

放到main方法里或者把tom加上static修饰符


查看完整回答
反对 回复 2023-04-17
  • 2 回答
  • 0 关注
  • 99 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信