1

Memo Solution

Soldier alpha;
Soldier bravo;

alpha = new Soldier(); //Create alpha
alpha.age = 21; //Assign 21 to the variable alpha.age

bravo = alpha //Assign alpha reference to bravo. bravo is now alpha not the copy.
bravo.age = 42 //Assign 42 to the variable bravo.age.

System.out.print("Alpha is" + alpha.age + "years old.");

Alpha is 42 years old.

I have 2 fragments. I would like them to use the same variable. It means if one fragment changes the variable I don't need to send it to the other to get it.

I have developed in C and for that I just need to send the variable's memory address.

What is the best way to do that in Java/Android ?

Thanks in advance :)

4 Answers 4

2

Make that variable an object that can be modified by setters. And then keep references to that Object in your fragments. Since your fragments will point to the same reference, any changes to the Object will be seen by both.

Sign up to request clarification or add additional context in comments.

3 Comments

The syntaxe of the object reference is himself ? I mean when I write function(Object). I have sent the Object's reference ? Or do I need something like function(&Object) like in C ^^
@nsvir yes, internally, Java passes Object references by value see this article for more details: javaworld.com/javaqa/2000-05/03-qa-0526-pass.html
0

Just create a private static instance and access it get/set with the two fragments.

But please go through the disadvantages of using static variables, if you planning to use it in multiple threads.

Comments

0

dkatzel's solution will work but I'm personally too lazy for setters and getters. The way I'm doing that: I have a Constants.java class where I store static variables (some of them aren't constants though) which I need to access from across the app. Works fine. Example:

 public static boolean isAboutShowing = false;
 public static final int TOAST_TYPE_OK = 0;

By the way, to access some class fields using setters and getters, you'll need an instance of that class. Which means 1 more Object stored in memory. That for sure won't really slow your app, but 1 more Object is 1 more Object :)

7 Comments

Everything about this is awful from a code management perspective. If you want any sort of encapsulation, you don't want these to be public or static.
@netinept and if I don't? For example I open the settings activity and set the static field isSettings to true. Now I can check if settings are open from anywhere in my app without needing any Object.
I personally think that using static fields is an advantage as long as you are not exaggerating
@Simon would you mind giving a concrete example where using static fields causes bugs? Android framework also has a lot of static constants and methods by the way
Static constants and methods, of course, but they are not an problem since there is no state. I am unaware of a single static variable in the framewwork. Concrete examples abound but try reading this c2.com/cgi/wiki?GlobalVariablesAreBad. The Singleton pattern is one possible answer.
|
0

As @dkatzel said, use an Object and modify it by setters. Here is an example of the Object's class:

public class News {

private String title;
private String content;

public void setTitle(String tit) {
    this.title = tit;
}

public void setContent(String cont) {
    this.content = cont;
}

public String getTitle() {
    return this.title;
}

public String getContent() {
    return this.content;
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.