teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

5

プライベートな static メンバ変数の使い道を追加

2021/10/17 16:52

投稿

blendthink
blendthink

スコア25

answer CHANGED
@@ -36,10 +36,12 @@
36
36
  }
37
37
  ```
38
38
 
39
- ちなみに、プライベートな static メンバ変数の使い道は以下ようSoundPlayer
39
+ ちなみに、プライベートな static メンバ変数の使い道は、SoundPlayer インスタンスを生成せずSoundPoolインスタンスを使いたい時などです。
40
40
 
41
- ```
41
+ 以下がその例です。(今回は分かりやすいように hitSound と overSound を除外して固定値にしています)
42
42
 
43
+ ```java
44
+ package jp.codeforfun.catchtheball;
43
45
 
44
46
  import android.media.AudioManager;
45
47
  import android.media.SoundPool;

4

プライベートな static メンバ変数の使い道を追加

2021/10/17 16:52

投稿

blendthink
blendthink

スコア25

answer CHANGED
@@ -34,4 +34,33 @@
34
34
  soundPool.play(overSound, 1.0f, 1.0f, 1, 0, 1.0f);
35
35
  }
36
36
  }
37
+ ```
38
+
39
+ ちなみに、プライベートな static メンバ変数の使い道は以下のように SoundPlayer の
40
+
41
+ ```
42
+
43
+
44
+ import android.media.AudioManager;
45
+ import android.media.SoundPool;
46
+
47
+ public class SoundPlayer {
48
+
49
+ private static SoundPool soundPool;
50
+
51
+ private static SoundPool getSoundPool() {
52
+ if (soundPool == null) {
53
+ soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
54
+ }
55
+ return soundPool;
56
+ }
57
+
58
+ public static void playHitSound() {
59
+ getSoundPool().play(1, 1.0f, 1.0f, 1, 0, 1.0f);
60
+ }
61
+
62
+ public static void playOverSound() {
63
+ getSoundPool().play(2, 1.0f, 1.0f, 1, 0, 1.0f);
64
+ }
65
+ }
37
66
  ```

3

訂正の訂正です。。深夜なので頭働いてないかもです。。

2021/10/17 16:49

投稿

blendthink
blendthink

スコア25

answer CHANGED
@@ -1,4 +1,37 @@
1
1
  private というアクセス修飾子が付いているため、static 変数で定義されていても外部から利用することはできません。よって、グローバル変数ではありません。
2
2
 
3
- ~~リンク先を拝見いたしましたが、そもそも static 変数である必要が無さそうです。~~
3
+ リンク先を拝見いたしましたが、そもそも static 変数である必要がありません
4
+
5
+ 以下のようにできます。
6
+
7
+ ```java
8
+ package jp.codeforfun.catchtheball;
9
+
10
+ import android.content.Context;
11
+ import android.media.AudioAttributes;
12
+ import android.media.AudioManager;
13
+ import android.media.SoundPool;
14
+
15
+ public class SoundPlayer {
16
+
17
+ private final SoundPool soundPool;
18
+ private final int hitSound;
19
+ private final int overSound;
20
+
21
+ public SoundPlayer(Context context) {
22
+
23
+ soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
24
+
25
+ hitSound = soundPool.load(context, R.raw.hit, 1);
4
- インスタンス生成時に static 変数にインスタンスを入れているため必要ですね。失礼しました????
26
+ overSound = soundPool.load(context, R.raw.over, 1);
27
+ }
28
+
29
+ public void playHitSound() {
30
+ soundPool.play(hitSound, 1.0f, 1.0f, 1, 0, 1.0f);
31
+ }
32
+
33
+ public void playOverSound() {
34
+ soundPool.play(overSound, 1.0f, 1.0f, 1, 0, 1.0f);
35
+ }
36
+ }
37
+ ```

2

ミスを訂正

2021/10/17 15:39

投稿

blendthink
blendthink

スコア25

answer CHANGED
@@ -1,3 +1,4 @@
1
1
  private というアクセス修飾子が付いているため、static 変数で定義されていても外部から利用することはできません。よって、グローバル変数ではありません。
2
2
 
3
- リンク先を拝見いたしましたが、そもそも static 変数である必要が無さそうです。。
3
+ ~~リンク先を拝見いたしましたが、そもそも static 変数である必要が無さそうです。。~~
4
+ → インスタンス生成時に static 変数にインスタンスを入れているため必要ですね。失礼しました????

1

文章の訂正

2021/10/17 15:12

投稿

blendthink
blendthink

スコア25

answer CHANGED
@@ -1,3 +1,3 @@
1
- private というアクセス修飾子が付いているため、static 変数で定義されていても外部から利用することはできないため、グローバル変数ではありません。
1
+ private というアクセス修飾子が付いているため、static 変数で定義されていても外部から利用することはできません。よって、グローバル変数ではありません。
2
2
 
3
3
  リンク先を拝見いたしましたが、そもそも static 変数である必要が無さそうです。。