Android はJava言語で開発します(Kotlinでもできます)。Javaではクラスを定義して呼び出せます。
Activity:A, Activity:B に共通するメソッドを別クラスで設定すると便利です。これはActivity 画面遷移とデータの受け渡しとは異なります。
 
例として簡単なテストアプリを作ってみます。ボタンを押したらテキストが表示され、テキストは別クラスから呼び出します。
クラスの定義
SubClassというクラスを作り、名前を呼び出すメソッドname() を作成します。
「File」「New」「Java Class」を選択します。
Kotlinの場合は「Kotlin File/Class」になりますが
![[Android]  クラス メソッド の設定と呼び出し 1x1.trans - [Android]  クラス メソッド の設定と呼び出し](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
ファイル名を入力
![[Android]  クラス メソッド の設定と呼び出し 1x1.trans - [Android]  クラス メソッド の設定と呼び出し](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
MainActivityと同じ個所に作成されました。
 
![[Android]  クラス メソッド の設定と呼び出し 1x1.trans - [Android]  クラス メソッド の設定と呼び出し](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
name() はフィールドの文字の配列から指定された要素を返すようにします。setter, getterですが、最近は古臭いとも言われますけど、まあ Javaなので。Kotlinのような最近の言語では使わなくなりました。
 
SubClass.java
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package your.package.name; class SubClass {     private final String[] nm = {"Apple","Google","MicroSoft"};     // コンストラクター     SubClass(){     }     String name(int number){         String res = "error";         // 配列要素外ではエラーとする         if(nm.length > number) {             res = nm[number];         }         return res;     } } | 
 
Activity として MainActivity があり
そこでボタンとテキスト表示を行います
 
MainActivity.java
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | //package your.package.name; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity {     private TextView textView;     private SubClass sc;     private int count = 0;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         // SubClass のインスタンス生成         sc = new SubClass();         // TextView の設定         textView = findViewById(R.id.text_view);         // ボタンを設定         Button button = findViewById(R.id.button);         // リスナーをボタンに登録         button.setOnClickListener(v -> {             // SubClass のメソッド name() を呼び出す             String str =  sc.name(count);             // TextView に張り付ける             textView.setText(str);             // カウントアップ             count++;         });     } } | 
 
レイアウトです
activity_main.xml
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:gravity="center"     android:padding="20dp"     android:background="#dfe"     tools:context=".MainActivity">     <TextView         android:id="@+id/text_view"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textSize="30sp"         android:textColor="#00f"         android:layout_margin="20dp"         android:text="@string/test" />     <Button         android:id="@+id/button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_margin="20dp"         android:text="@string/button" /> </LinearLayout> | 
 
リソースです
| 1 2 3 4 5 | <resources>     <string name="app_name">YourAppName</string>     <string name="test">Test</string>     <string name="button">Button</string> </resources> | 
これはメソッドの引数として int を入れましたが
よくあるケースとして
Activity の Context を渡す場合です
その場合は
 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class MainActivity extends AppCompatActivity {     private SubClass sc;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         Context context = getApplicationContext();         sc = new SubClass(context);       ...     } } | 
 
SubClass のコンストラクタで受けます
| 1 2 3 4 5 6 7 8 9 10 | class SubClass {     private Context con;     SubClass(Context context){         this.con = context;     } ... } | 
 
こんな感じです
