0

Could someone explain to me one thing about call methods, viz. When I was learning at Neatbeans, calling a method was always done using a reference variable, where I had to create a real object before, for example:

public class Question {


    public static void main(String[] args) {

        Test test = new Test();
        test.method();

    }

}

class Test {

    void method() {

        System.out.println("Test");
    }
}

In this case, I had to create an object, assign its reference to the test variable, and then call the method.

However, in Android Studio, to call a method, I do not have to create a reference variable or an object, I only directly call the method ... for example:

public class SecondActivity extends AppCompatActivity {

    EditText editText;
    Button button2;
    String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        editText = findViewById(R.id.editText);
        button2 = findViewById(R.id.button);

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                prepareResult();
            }
        });

    }

    public void prepareResult() {

        Intent i = new Intent();
        name = editText.getText().toString();
        i.putExtra("name", name);
        setResult(RESULT_OK, i);
        finish();
    }
}

In this case, I do not create an object, and I do not assign its reference to 'X' variables, I immediately call the prepareResult method. Why is this happening?

UnknownBoy
  • 169
  • 7

3 Answers3

0

In Java, when you call another method in the same class, you do not need to reference by it's class object.

Where when you call the method from same class, you can access method directly.

Rahul SK
  • 350
  • 3
  • 23
0

It's all a matter of scope. In your first example, you were trying to use the method method() from the class Test from within another class, Question.

In the second example, you can call prepareResult() directly because the method from where that call is issued, onCreate(), belongs to the same class SecondActivity.

This is possible because, essentially, they are in the same scope. All methods and variables in a particular class are visible amongst each other. Visibility outside of the class depends on the access modifiers, public, private, protected or the default package-private

You can find more details in this Oracle Java tutorial:

https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

mjuarez
  • 16,372
  • 11
  • 56
  • 73
  • I am sorry, I gave an inadequate example to my intentions, I was concerned about the situation when the method() would be in the Question class, then I would not be able to call it. But i was looking for more information, and the answer to this problem is simply the fact that main () is a static method, so it can only call static methods? – UnknownBoy Jul 12 '19 at 01:56
  • Yes, from a _static_ context, you can only call directly other static methods. If your method definition would have been `static void method()`, then you would have been able to call it from `Question` with `Test.method()`, without using `new`. – mjuarez Jul 12 '19 at 02:04
0

In Java, whenever you call a function which is outside the activity, you are calling. You have to use a reference variable to call the other class constructor to build an object. It can be done in your case. Test test = new Test(); test.method(); OR new Test().method();

Whenever you call a function which is inside the same class(within where you defined the function), you can call directly using its name directly because you do not have to call the constructor of the class which is already created. Like you have done in your other code.

You can see the class do not change here.

public class SecondActivity extends AppCompatActivity {

    EditText editText;
    Button button2;
    String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        editText = findViewById(R.id.editText);
        button2 = findViewById(R.id.button);

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                prepareResult();
            }
        });

    }

    public void prepareResult() {

        Intent i = new Intent();
        name = editText.getText().toString();
        i.putExtra("name", name);
        setResult(RESULT_OK, i);
        finish();
    }
}