Silentbob
Silentbob

Reputation: 3065

Using a class without creating a new instance of it

Newish to Java and very new to Android development.

I have followed the following tutorial - Android tutorial (Basic Hello World App) and I am now changing it slightly as a proof of concept.

Basically I want to use a class I have created but I am having some difficulties. The class is shown below.

public class Employee {

    private HashMap<String, String> employees = new HashMap<>();

    public void setEmployees(String name, String jobTitle) {
        employees.put(name, jobTitle);
        System.out.println(employees);
    }

    public String getEmployees(String name){
        return employees.get(name);
    }

}

I populate the HashMap from MainActivity.java. Using the set method above, this works as expected. I have tested it and I can see the HashMap has the required number of entries.

My problem is when getting the data back. How do I use the class. I have a file name DisplayMessageActivity.java and the following code within it.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;


public class DisplayMessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);

        // Get the Intent that started this activity and extract the string
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Capture the layout's TextView and set the string as its text
        TextView employee_name = findViewById(R.id.employee_Name);
        employee_name.setText(message);
        TextView employee_title = findViewById(R.id.employee_Title);
        employee_title.setText(employee.getEmployees(message));
    }
}

The last line is where I am getting the error. This is because it doesnt know what employee is. I presume I need to add:

Employee employee = new Employee;

If I add this within the onCreate method it creates a new instance and therefore it has new values. I have also added it just above onCreate with the same results.

What am I missing?

Upvotes: 0

Views: 77

Answers (3)

savepopulation
savepopulation

Reputation: 11921

You can use Singleton in-memory cache to keep your employees.

public class Employee {

    private static sInstance;

    private HashMap<String, String> employees = new HashMap<>();

    private Employee(){
        // No instance available
    }

    public static synchronized Employee getInstance(){
        if(sInstance == null){
            sInstance = new Employee();
        }    
        return sInstance;
    }

    public void setEmployees(String name, String jobTitle) {
        employees.put(name, jobTitle);
        System.out.println(employees);
    }

    public String getEmployees(String name){
        return employees.get(name);
    }
}

Later you can use your in-memory cache like the following:

Employee.getInstance().getEmployees(message);

Upvotes: 0

Abdul Waheed
Abdul Waheed

Reputation: 4678

Replace your Employee class code with below one

public class Employee {

private static HashMap<String, String> employees;

public Employee() {
    if (employees == null) {
        employees = new HashMap<>();
    }
}

public void setEmployees(String name, String jobTitle) {
    employees.put(name, jobTitle);
    System.out.println(employees);
}

public String getEmployees(String name) {
    return employees.get(name);
}
}

Hope that helps you.

Upvotes: 0

Notsileous
Notsileous

Reputation: 560

To retain the data you would want to make the variable and the methods static

public class Employee {

private static HashMap<String, String> employees = new HashMap<>();

public static void setEmployees(String name, String jobTitle) {
    employees.put(name, jobTitle);
    System.out.println(employees);
}

public static String getEmployees(String name){
    return employees.get(name);
}

}

This means that only one version can exist at a time. You would call the class directly and the method.

employee_title.setText(Employee.getEmployees(message))

Upvotes: 1

Related Questions