Reputation: 49
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
Upvotes: 1
Views: 70
Reputation: 1190
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.
Upvotes: 1
Reputation: 1976
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
Upvotes: 0
Reputation: 3688
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.
Upvotes: 0