public class Test1 { public static void main(String[] args) { Test1 test = new Test1("ABC"); System.out.println(test.text.toLowerCase()); } private String text; public Test1(String s) {
String text = s;
}}
为什么会引起这个错误?

解决方案 »

  1.   

    构造方法问题,你是在构造方法里重新定义了一个局部的text,应该这样public class Test1 {public static void main(String[] args) {Test1 test = new Test1("ABC");System.out.println(test.text.toLowerCase());}private String text;public Test1(String s) {
    text = s;
    }}
      

  2.   

    String text = s;
    此text非彼text!
      

  3.   

    private String text; public Test1(String s) {
     String text = s;
     }}
    上面定义的text没有用到。你在构造方法里又实例一个。他们是不同的。你每次实例都是一个新的对象。
    一般构造方法都要有个无参的,据说是规范
    public Test1(){}
      

  4.   

    private 的修饰符 test.text.toLowerCase() 能得到吗? 编译器不报错吗?
      

  5.   

    构造函数里text就不要加String 
      

  6.   

    public class Test1 {public static void main(String[] args) {Test1 test = new Test1("ABC");System.out.println(test.text.toLowerCase());}private String text;public Test1(String s) {
    this.text = s;
    }}
      

  7.   

    把“String ”换成 this.或者 把 那个删掉就行了
      

  8.   


    public Test1(String s) {
    String text = s;
    }

    这个构造方法里的text和

    private String text;

    是两个不同的变量,方法里的text的作用域是{}内,而成员变量的作用域是整个包内(默认情况下),如果在方法内或方法传递的参数内又定义了同成员变量同名的变量明时,Java会用this.变量名 来代替成员变量,因此你的构造方法应该这样写public Test1(String s) {
    this.text = s;
    }
      

  9.   

    你又多声明了一个 test ,额