Skip to content

How to access values of variables from other class?

Hey guys so I am new to OOP and trying to make my first OOP program. But then I encountered a problem. I don’t know why the values hasn’t changed even though I put a variable on the parameter of the constructor.

Here is my code:

public class Phone {

    public static void main(String[] args) {
        addContacts();
        viewContacts();
        Contacts cont = new Contacts();

    }
    public static void addContacts(){
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Number: ");
        int number = scan.nextInt();
        scan.nextLine();
        System.out.print("Enter name: ");
        String name = scan.nextLine();
        Contacts contacts = new Contacts(name,number);

    }
    public static void viewContacts(){
        Contacts contacts =  new Contacts();
        System.out.println("All contacts");
        System.out.println("Number: " + contacts.getNumber());
        System.out.println("Name: "+ contacts.getName());
    }
}

Here is the other class:

public class Contacts {
    private String name, message;
    private int number;


    public Contacts(String name, int number) {
        this.name = name;
        this.number = number;
    }
    public Contacts(){

    }

    public String getName() {
        return name;
    }


    public int getNumber() {
        return this.number;
    }

}

Answer

The problem is that you’re creating a new Contacts in each method, which means a Contacts is born in that method and dies when the method ends.

A possible solution is to create a static Contacts variable in Phone. It’s important that it is declared as static because you’ll then use it in static methods.

To use it in the methods, simply refer to contact without declaring a new one.

public class Phone {

    static Contacts contacts;

    public static void main(String[] args) {
        addContacts();
        viewContacts();

    }
    public static void addContacts(){
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Number: ");
        int number = scan.nextInt();
        scan.nextLine();
        System.out.print("Enter name: ");
        String name = scan.nextLine();
        contacts = new Contacts(name,number); // this is the class' Contact

    }
    public static void viewContacts(){
        System.out.println("All contacts");
        System.out.println("Number: " + contacts.getNumber()); // this is the class' Contact
        System.out.println("Name: "+ contacts.getName()); // this is the class' Contact
    }
}

An even better solution would be for addContacts to return a new Contacts and then pass that to viewContacts, like this:

public class Phone {

    public static void main(String[] args) {
        Contacts contacts = addContacts();
        viewContacts(contacts);
    }

    public static Contacts addContacts(){
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Number: ");
        int number = scan.nextInt();
        scan.nextLine();
        System.out.print("Enter name: ");
        String name = scan.nextLine();
        return new Contacts(name,number);

    }

    public static void viewContacts(Contacts contacts){
        System.out.println("All contacts");
        System.out.println("Number: " + contacts.getNumber());
        System.out.println("Name: "+ contacts.getName());
    }
}

Finally (but we’re in nitpick territory at this point) Contacts seems to represent a single contact, so it should really be called Contact and addContacts should be called createContact or populateContact.