Java make an object visible across methods -


i have methods

a:

string teststring = new string ("blublub"); 

b:

system.out.println(teststring); 

what have b can see object of a?

i tried public || final stuff wasn't right way think.

thx help

whole code of 2 methods

public void onwindowfocuschanged(boolean hasfocus){     if (hasfocus){         final string teststring= new string ("blubblub");     } }   public void a() {     system.out.println(teststring); } 

error log when putting mediaplayer outside method.

04-06 05:20:25.140: e/androidruntime(12120): java.lang.runtimeexception: unable instantiate activity componentinfo{~.mainactivity}: java.lang.nullpointerexception 04-06 05:20:25.140: e/androidruntime(12120):    @ android.app.activitythread.performlaunchactivity(activitythread.java:1803) 04-06 05:20:25.140: e/androidruntime(12120):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:1919) 04-06 05:20:25.140: e/androidruntime(12120):    @ android.app.activitythread.access$1500(activitythread.java:160) 04-06 05:20:25.140: e/androidruntime(12120):    @ android.app.activitythread$h.handlemessage(activitythread.java:1008) 04-06 05:20:25.140: e/androidruntime(12120):    @ android.os.handler.dispatchmessage(handler.java:130) 04-06 05:20:25.140: e/androidruntime(12120):    @ android.os.looper.loop(sourcefile:351) 04-06 05:20:25.140: e/androidruntime(12120):    @ android.app.activitythread.main(activitythread.java:4070) 04-06 05:20:25.140: e/androidruntime(12120):    @ java.lang.reflect.method.invokenative(native method) 04-06 05:20:25.140: e/androidruntime(12120):    @ java.lang.reflect.method.invoke(method.java:538) 04-06 05:20:25.140: e/androidruntime(12120):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:906) 04-06 05:20:25.140: e/androidruntime(12120):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:664) 04-06 05:20:25.140: e/androidruntime(12120):    @ 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):    @ android.media.mediaplayer.create(mediaplayer.java:697) 04-06 05:20:25.140: e/androidruntime(12120):    at~.mainactivity.<init>(mainactivity.java:79) 

line 79 is: private mediaplayer mediaplayerw = mediaplayer.create(getapplicationcontext(), r.raw.soft);

around line 79 is:

 button3 = (button)findviewbyid(r.id.button3);         button3.setonclicklistener(this); }   private mediaplayer mediaplayerw = mediaplayer.create(getapplicationcontext(), r.raw.soft);  public void onwindowfocuschanged(boolean hasfocus)  

is possible, can have sth getbasecontext?

in code:

public void onwindowfocuschanged(boolean hasfocus){     if (hasfocus){         final string teststring= new string ("blubblub");     } }      public void a() {     system.out.println(teststring); } 

while teststring declared "in class" declared inside of onwindowfocuschanged method of class, , doing this, visible inside of method. make visible throughout class, declare in class , not in method or constructor:

public class myclass {    // variable below declared *in* class    // , visible throughout class    private string teststring = "";    private string anothervariable; // declared not instantiated   public void onwindowfocuschanged(boolean hasfocus){     if (hasfocus){         // don't redeclare variable here, , don't use new string(...)         // final string teststring= new string ("blubblub");           anothervariable = "blubblub"; // instantiated here     } }      public void somemethod() {       // variable visible inside of non-static methods       system.out.println(teststring);    }  } 

in addition, want avoid using string mystring = new string("foo"); since can cause possibly inefficient creation of unnecessary objects. instead use string mystring = "foo"; re-use strings string pool if 1 available.


Comments