简体   繁体   中英

How to use setters and getters in different java file

This is my setters and getters in ShoppingListName.java

public class ShoppingListName {
    private String shoppingListName;

    public String getShoppingListName() {
        return shoppingListName;
    }

    public void setShoppingListName(String shoppingListName) {
        this.shoppingListName = shoppingListName;
    }
}

I did this in MainMenu.java

ShoppingListName shoppingList = new ShoppingListName();
shoppingList.setShoppingListName(inputListName.getText().toString());

And I'm trying to pass the string to a different java file (CreateList.java) How can I do that?

I tried this.

ShoppingListName shoppingList = new ShoppingListName();
Log.e("ShoppingListName", "" + shoppingList.getShoppingListName());

But obvious I'm receiving a null return.

And I'm trying to pass the string to a different java file (CreateList.java) How can I do that?

Presumably, what you mean here is that you'd like to pass the String to a method defined in the class CreateList .

So just create a method on CreateList that takes a String , and does whatever it needs to do.

public class CreateList {

    public static void doSomething(String listName) { 
       //do whatever you want with it
    }
}

As a side note, your class ShoppingListName doesn't seem to model any kind of meaningful real world object. Consider changing it to :

public class ShoppingList {

    List<ShoppingItems> items;
    String listName;
 ..   
 ..
}

The same goes for CreateList . What is the primary responsibility for this object?

ShoppingListName class instance in MainMenu.java is different from the instance in CreateList.java . Obviously the new instance in CreateList.java cannot get the value you have set in the instance in MainMenu.java . You have to somehow pass the instance from MainMenu.java to CreateList.java or share the same instance between the two classes.

* Getter Setter Class

public class ShoppingListName {
private static String shoppingListName;
public static String getShoppingListName() {
    return shoppingListName;
}
public static void setShoppingListName(String shoppingListName) {
    ShoppingListName.shoppingListName = shoppingListName;
}

}

To set the value ShoppingListName.setShoppingListName("the value that u need to set");

To get the value String value=ShoppingListName.getShoppingListName(); *

UPDATE

as @laalto mentioned in the comment. separate pojo class with static parameter to store data for some extend is not the perfect solution.

instead of that according to me, If you want to save the data for a long time save it in shared preferences otherwise create a non static variable in application class and use like this

public class HelloApplication extends Application {
        private int globalVariable=1;

        public int getGlobalVariable() {
                return globalVariable;
        }

        public void setGlobalVariable(int globalVariable) {
                this.globalVariable = globalVariable;
        }
        @Override
        public void onCreate() {
                //reinitialize variable
        }
}

And in your Activity, do this,

(HelloApplication)getApplication()).setGlobalVariable(10);
int valiable=((HelloApplication)getApplication()).getGlobalVariable();

You could make getter and setter methods static and then that last call you used would work. Then, provided the flow is such that get method is called after set method, you would'nt have to explicitly pass anything.

Thus, effectively , you would be sharing the single instance across as many other classes as you want.

try this code :

public class ShoppingListName extend Application{
    private String shoppingListName;

    public String getShoppingListName() {
        return shoppingListName;
    }

    public void setShoppingListName(String shoppingListName) {
        this.shoppingListName = shoppingListName;
    }
}

in Android Manifest.xml :

<application
    android:name="yourPacket.ShoppingListName"
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="yourPacket.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

in MainMenu.java :

ShoppingListName shoppinglistname = (ShoppingListName) MainMenu.this.getApplicationContext();
// Now you can get and set shoppinglistname object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM