1

I have this class

public class vartotojas {
String elpastas, slaptazodis;
public vartotojas (String elpastas,String slaptazodis)
  {
    this.elpastas = elpastas;
    this.slaptazodis = slaptazodis;
  }
}

And I am trying to use variable in this class

public class saugykla {
  public static final String SP_name = "vartotojoduomenys";
  SharedPreferences saugykladb;
  public saugykla (Context context){
    saugykladb = context.getSharedPreferences(SP_name, 0);
  }
  public void saugoti (vartotojas user){
    SharedPreferences.Editor SP_editor = saugykladb.edit();
    SP_editor.putString("elpastas", vartotojas.elpastas);
  }
}

I get error on the last line vartotojas.elpastas what I am doing wrong after I put dot after vartotojas I get no option to choose elpastas I am fallowing tutorial on youtube I did exactly the save as youtuber did... I get error : error: non-static variable elpastas cannot be referenced from a static context

Akash Rajbanshi
  • 1,553
  • 11
  • 23
ben
  • 39
  • 1
  • 8
  • possible duplicate of [static variable vs non static variable](http://stackoverflow.com/questions/19035916/static-variable-vs-non-static-variable) – Nir Alfasi May 25 '15 at 04:54
  • Is there any reason you did not create a public getter on `vartotojas`: meaning you add `public String getElpastas(){return elpastas}` or make `String elpastas` public static – Kenneth Clark May 25 '15 at 04:55
  • 2
    You probably meant `user.elpastas` and please give classes CamelCase names. – Thilo May 25 '15 at 04:55

3 Answers3

1

Replace class name with instance name:

SP_editor.putString("elpastas", vartotojas.elpastas);

                         |
                         V

SP_editor.putString("elpastas", user.elpastas);

For you to be able to access class method through class name, the method must be static.

Suspended
  • 1,184
  • 2
  • 12
  • 28
0

First of all you should familiarize yourself with Java Naming Conventions, it'll make your code a lot more readable (for instance, having your class names start with a capital letter).

Second, you're referencing the class name when you should be referencing the instance name. In your case, it is user passed as an argument to the method, so:

public void saugoti (vartotojas user){
    SharedPreferences.Editor SP_editor = saugykladb.edit();
    SP_editor.putString("elpastas", user.elpastas);
}

But that won't be enough. user can't access elpastas variable. You either need to make it non-private (I'm not sure which packages they're in in your code), or the more preferred way, add some getters to your vartotojas class.

public class vartotojas {

    String elpastas, slaptazodis;

    public vartotojas (String elpastas,String slaptazodis) {
        this.elpastas = elpastas;
        this.slaptazodis = slaptazodis;
    }

    public String getElpstas() {
        return this.elpastas;
    }

    public String getSlaptazodis() {
        return this.slaptazodis;
    }
}

And then, you'll need to access the variable through the getter:

public void saugoti (vartotojas user){
    SharedPreferences.Editor SP_editor = saugykladb.edit();
    SP_editor.putString("elpastas", user.getElpastas());
}

P.S. You should also think if you want to add setter method, or if initialization in the constructor will suffice in your case.

Hope this helps.

Ori Lentz
  • 3,668
  • 6
  • 22
  • 28
0

You are close but having small mistake about local variable.

public void saugoti (vartotojas user){
    SharedPreferences.Editor SP_editor = saugykladb.edit();
    SP_editor.putString("elpastas", vartotojas.elpastas);
}

when you localize the variable user for class i.e. vartotojas user in method why are you using like vartotojas.elpastas change the line like:

SP_editor.putString("elpastas", user.elpastas);

MORE

How you can access this variable like class.field read static Here. This is basically whole new concept and you must know it (if you don't)

Also I would like to recommend to read and follow Java Code Conventions

Sarz
  • 1,970
  • 4
  • 23
  • 43