Grammar that uses simple this to transfer factors in Java

Asked 2 years ago, Updated 2 years ago, 93 views

Hello, I am posting something that I don't understand among Java grammar.

Yes) GoogleApiClient gac = new GoogleApiClient.Builder(this, this, this)
.addApi(Games.API)
.build();

I'm inquiring because I don't understand grammatically how to convey this as a factor as above.

this.Parameters : refers to the value of one's own parameters this(): Invoke your own constructor

It's a little unfamiliar to deliver the factor using just this

I'd appreciate it if you could give me an example source or explanation that I can easily understand

java this

2022-09-21 14:47

1 Answers

We've created a simple example source.

@Test Attached is the starting point. You can think of it as the main method.

public class ExampleClass {

    private String className = "ExampleClass";

    public String getClassName() {
        return className;
    }

    @Test
    public void startPotnt() throws Exception {
        TestClass testClass = new TestClass(this);
        /*
        This refers to one's own object.
        I delivered the object of the Sample Class to the TestClass constructor with this
        TestClass constructor must receive as SampleClass type
        */
    }

}

class TestClass {

    private String className;

    public TestClass(ExampleClass exampleClass) {
        String className= exampleClass.getClassName();
        this.className= className;
        System.out.println(this.className); // ExampleClass

        /*
        The current TestClass has a className of 
        1. TestClass's className member variable and 
        2. Two className region variables that store the value of exampleClass.getClassName();
        To distinguish between the two, use this to point to your own object
        The value of className is stored in this.className (which refers to the TestClass member variable).
        */
    }

}


2022-09-21 14:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.