Java 使对象在方法中可见

标签 java object

我必须有方法

答:

String teststring = new String ("blublub");

B:

System.out.println(teststring);

我必须做什么才能让 B 看到 A 的那个对象?

我已经尝试过公开 ||最后的东西,但我认为这不是正确的方式。

感谢您的帮助

两个方法的完整代码

public void onWindowFocusChanged(boolean hasFocus){
    if (hasFocus){
        final String teststring= new String ("blubblub");
    }
}


public void a() {
    System.out.println(teststring);
}

将 MediaPlayer 置于方法外部时出现错误日志。

04-06 05:20:25.140: E/AndroidRuntime(12120): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{~.MainActivity}: java.lang.NullPointerException
04-06 05:20:25.140: E/AndroidRuntime(12120):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1803)
04-06 05:20:25.140: E/AndroidRuntime(12120):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1919)
04-06 05:20:25.140: E/AndroidRuntime(12120):    at android.app.ActivityThread.access$1500(ActivityThread.java:160)
04-06 05:20:25.140: E/AndroidRuntime(12120):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1008)
04-06 05:20:25.140: E/AndroidRuntime(12120):    at android.os.Handler.dispatchMessage(Handler.java:130)
04-06 05:20:25.140: E/AndroidRuntime(12120):    at android.os.Looper.loop(SourceFile:351)
04-06 05:20:25.140: E/AndroidRuntime(12120):    at android.app.ActivityThread.main(ActivityThread.java:4070)
04-06 05:20:25.140: E/AndroidRuntime(12120):    at java.lang.reflect.Method.invokeNative(Native Method)
04-06 05:20:25.140: E/AndroidRuntime(12120):    at java.lang.reflect.Method.invoke(Method.java:538)
04-06 05:20:25.140: E/AndroidRuntime(12120):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:906)
04-06 05:20:25.140: E/AndroidRuntime(12120):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:664)
04-06 05:20:25.140: E/AndroidRuntime(12120):    at dalvik.system.NativeStart.main(Native Method)
04-06 05:20:25.140: E/AndroidRuntime(12120): Caused by: java.lang.NullPointerException
04-06 05:20:25.140: E/AndroidRuntime(12120):    at android.media.MediaPlayer.create(MediaPlayer.java:697)
04-06 05:20:25.140: E/AndroidRuntime(12120):    at~.MainActivity.<init>(MainActivity.java:79)

第 79 行是: private MediaPlayer mediaPlayerW = MediaPlayer.create(getApplicationContext(), R.raw.soft);

第 79 行周围是:

 button3 = (Button)findViewById(R.id.button3);
        button3.setOnClickListener(this); }


private MediaPlayer mediaPlayerW = MediaPlayer.create(getApplicationContext(), R.raw.soft);
 public void onWindowFocusChanged(boolean hasFocus) 

它有可能与 getBaseContext 有关吗?

最佳答案

在此代码中:

public void onWindowFocusChanged(boolean hasFocus){
    if (hasFocus){
        final String teststring= new String ("blubblub");
    }
}    

public void a() {
    System.out.println(teststring);
}

虽然 testString 是在“类中”声明的,但它实际上是在类的 onWindowFocusChanged 方法内部声明的,通过这样做,它仅在该方法内部可见。要使其在整个类中可见,请在类中而不是在方法或构造函数中声明它:

public class MyClass {
   // variable below declared *in* the class
   // and is visible throughout the class
   private String testString = "";
   private String anotherVariable; // declared but not instantiated


public void onWindowFocusChanged(boolean hasFocus){
    if (hasFocus){
        // don't redeclare the variable here, and don't use new String(...)
        // final String teststring= new String ("blubblub"); 

        anotherVariable = "blubblub"; // instantiated here
    }
}


   public void someMethod() {
      // variable is now visible inside of all non-static methods
      System.out.println(testString);
   }

}

此外,您希望避免使用String myString = new String("Foo");,因为这可能会导致不必要的对象创建效率低下。而是使用 String myString = "foo"; ,它将重新使用字符串池中的字符串(如果可用)。

关于Java 使对象在方法中可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15846543/

相关文章:

javascript - 为什么需要在对象中使用 this.property=property ?

java - Junit 4 套件被忽略(Eclipse 和 Maven)

java - 在 Java 中存储 1000 位数字的最佳数据类型

java - Spring Boot 忽略来自外部 application.properties 的日志记录级别

python - 区分组中的对象

JavaScript Object vs Map - 如何处理特殊键?

javascript - 使用方法将对象转换为 Javascript 中的字符串(适用于 Photoshop)

object - 如何判断对象是否是 AWS S3 上的文件夹

java - 是否可以在 while 循环中简洁地使用复杂的条件?

java - 通过 Java 套接字进行服务器间通信