Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8179511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:00:58+00:00 2026-06-07T00:00:58+00:00

I’m about 6 weeks into learning Java and still struggling with implementing static methods

  • 0

I’m about 6 weeks into learning Java and still struggling with implementing static methods (thought I really understood it, but this proved me wrong!).

I’m trying to make the value of a locally stored key-value pair available publicly. Here’s my initial code:

public class Settings extends Activity implements OnClickListener {
        public Settings(TextView loginText, TextView passwdText) {
    super();
    this.loginText = loginText;
    this.passwdText = passwdText;
}

public static String getDriverNum() {
    SharedPreferences sp = getSharedPreferences(DELEX_SP, MODE_PRIVATE); <---ERROR
    String Login = sp.getString("KeyLgn", "No Login Found");
    return Login;
}

Of course, I get an error “Cannot make a static reference to the non-static method getSharedPreferences(String, int) from the type ContextWrapper”, so I try to wrap the non-static method in my own public method, as a similar StackOverflow answer indicated:

public class Settings extends Activity implements OnClickListener {
public Settings(TextView loginText, TextView passwdText) {
    super();
    this.loginText = loginText;
    this.passwdText = passwdText;
}

public static String getDriverNum() {
    String Login = getSharedPref().getString("KeyLgn", "No Login Found"); <-- SAME ERROR
    return Login;
}

public  SharedPreferences getSharedPref() {
    SharedPreferences sp = getSharedPreferences(DELEX_SP, MODE_PRIVATE);
    return sp;
}

But this just caused the same error as I haven’t resolved the call to the non-static getSharedPreferences method. What’s the best way to resolve this? Is it to create a class that wraps getSharedPreferences instead?

Thanks for your patience while I struggle with static nomenclature.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:01:00+00:00Added an answer on June 7, 2026 at 12:01 am

    If you want to write a static method that utilizes non-static methods, you just pass an instance to it like this:

    public static void invokeMethod(SomeObject foo) {
        foo.bar();
    }
    

    So what you’re doing is a great pattern. I use it all the time for “helpers” that can be reused across many classes (aka composition) Just make your “SomeObject” the Context.

    Here’s the pattern I use in Android to get a nice central point to define default preferences:

    import android.content.Context;
    import android.content.SharedPreferences;
    import android.preference.PreferenceManager;
    
    import java.util.HashMap;
    /**
     * Provides support functions for getting preferences as well as a
     * central place for storing default preferences.
     */
    public final class PreferencesHelper {
        public static class Preferences {
            public static final String SOME_SETTING = "SOME_SETTING";
        }
    
       /** This allows us to define our default preferences all in one place */
        private static HashMap<String, Object> sDefaultPreferences =
            new HashMap<String, Object>(){{
            put(Preferences.SOME_SETTING, "value");
        }};
    
        public static SharedPreferences getDefaultPreferences(Context context) {
            return PreferenceManager.getDefaultSharedPreferences(context);
        }
    
        public static String getString(SharedPreferences prefs, String key) {
            return prefs.getString(key, (String) sDefaultPreferences.get(key));
        }
    
        /* Convenience method (use when getting only one preference) */
        public static String getString(Context context, String scanner) {
            SharedPreferences prefs = getDefaultPreferences(context);
            return getString(prefs, scanner);
        }
    

    …

    This pattern allows the definition of default values to be in one place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.