Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Newb question: Using the same object over two methods?

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Newb question: Using the same object over two methods?

    Hi guys,

    I'm a bit of a newbie to Java and was wondering if someone here may be able to help.

    I've had a few tries at typing out an explanation of what I mean and failed miserably so I will post the code

     */
    	public void onLocationChanged(Location location) {
                    //Some non relevant code removed//
     
    	        SharedPreferences app_preferences_write = getSharedPreferences(PREFS_NAME, 0);
    	        SharedPreferences.Editor e = app_preferences_write.edit();
     
    	        String latString = Double.toString(lat);
    	        String lngString = Double.toString(lng);
     
    	        e.putString("latitude", latString);
    	        e.putString("longitude", lngString);
    		}
     
           public void savePosition() {
                 e.commit();
    }

    My problem is that my object 'e' cannot be seen from savePosition() and I cannot commit in the onLocationChanged method as this would save the position every single time a new position is recorded.

    Please can someone offer some advice?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Newb question: Using the same object over two methods?

    my object 'e' cannot be seen from savePosition()
    You need to move the definition of e to put it in scope for the savePosition() method.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Newb question: Using the same object over two methods?

    Thanks for the response but I'm still not sure I understand.

    I define 'e' in the onLocationChanged method and then use e.putString to 'save' it to the object. If I define 'e' again in savePosition() won't that create a new object and therefore not have the data available to save when using e.commit()?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Newb question: Using the same object over two methods?

    If I define 'e' again in savePosition() won't that create a new object
    Yes every time you define a variable in its own scope, a new object will be created.
    therefore not have the data available to save when using e.commit()?
    If the e.commit() call uses a different object than the one with the data, then the data won't be there.

    The same variable e should be in scope for the two methods that use it.
    Right now the first e is local to the first method.

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Newb question: Using the same object over two methods?

    Wow, thanks for the fast response.

    Okay, sorry if I sound ignorant but if every time I define 'e' to be able to access it within different methods then really what my question should be is, what is the best way of passing or reading the lngString and latString variables from onLocationChanged?

    I've tried moving the entire 'e' object into the savePosition() method and then defining the strings as 'final' thought I am still unable to read them.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Newb question: Using the same object over two methods?

    define 'e' to be able to access it within different methods
    Make e a class variable. Then all methods can see it.
    Recommendation: Change the name to a meaningful one vs a single letter.

Similar Threads

  1. Question to do with methods.
    By Dragonkndr712 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 31st, 2011, 11:56 PM
  2. [SOLVED] newb help..
    By Hallowed in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 05:15 PM
  3. [SOLVED] Accessing methods of an object inside in ArrayList
    By jmack in forum Java Theory & Questions
    Replies: 3
    Last Post: January 22nd, 2011, 12:28 PM
  4. newbie question about Abstract methods
    By FailMouse in forum Java Theory & Questions
    Replies: 3
    Last Post: August 10th, 2010, 11:51 PM
  5. A question about inherited methods
    By joachim89 in forum Object Oriented Programming
    Replies: 1
    Last Post: January 12th, 2010, 09:06 PM