0

I'll get straight to the point. Im still learning a bit of syntax and want to know what the difference between this code is

CODE A:

 public class Buttonz extends JButton{

        public Buttonz(){
            setText(new String(String.valueOf(i)));
        }

    }

Please ignore the fact that i is undeclared, that is not where I am lost.

CODE B:

public class Buttonz extends JButton{

    public Buttonz(){
        setText(new String(String.this.charAt(i)));
    }

}

What I don't yet understand is what the difference is in typing String.this and String.

I was under the assumption that when I use the dot operator on a class I am accessing it's static methods(and/or variables if they're not hidden).

I have studied this a little bit and have concluded that when using String. I am accessing String static methods.. but when using String.this. I'm accessing the methods that my class Buttonz is extending.

Is this correct?

I apologize if I am right and are wasting time. I need to be sure to move on. Thank you.

Space Ghost
  • 765
  • 2
  • 13
  • 26

1 Answers1

0

String.this is the "qualified this" syntax. It is used to refer to either this, if the current class is String (which it is not in this case), or if this is a local or anonymous class (a type of inner class), the this of an enclosing class named String that contains this class. You have not showed us if this class is inside another class; I doubt that is the case. But even if it is, the enclosing class named String would not be java.lang.String, it must be some other class named String, since you cannot add code into existing classes. And then, I highly doubt it would have a method named charAt() which returns something which can be passed to new String(). So in conclusion, this is not real code.

newacct
  • 119,665
  • 29
  • 163
  • 224