Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
What is the type of this kind of declaration?

I am trying to learn Android. I have some Java experience but have never seen a code block like this one:

        addNumsButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            EditText firstNumEditText = (EditText) findViewById(R.id.firstNumEditText);
            EditText secNumEditText = (EditText) findViewById(R.id.secNumEditText);
            TextView resultTextView = (TextView) findViewById(R.id.resultTextView);
            resultTextView.setText((Integer.parseInt(firstNumEditText.getText().toString()) + Integer.parseInt(secNumEditText.getText().toString())) + "");
        }
    });

What is getting declared after the View.OnClickListener()? I checked that View.OnClickListener() returns type Interface.

What does the code after that method is used for?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

That's an anonymous class. It's special syntax for creating an instance of an abstract type by providing implementations right at the point of declaration. It's very common in GUI code (Android, Swing, what have you) to provide GUI action callbacks.

What your snippet is doing is pass an ad-hoc instance of View.OnClickListener to setOnClickListener that executes the code in the innermost brace block when the button is clicked.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!