0

OK guys, up till now (and since I'm a beginner) I was programming Java based on procedural programming and it was nice and all but It's time to use Java like-a-boss.

I'm learning the OOP concept now while writing some code as practice. What I dont understand is that if I create a few objects this way:

    Contact first = new Contact(25, "Yosi", "Male");
    System.out.println("Age of contact " + first.toString() + " is - "
            + first.getAge() + " " + first.getName());

    Contact second = new Contact(22, "lisa", "Femal");
    System.out.println("Age of contact " + second.toString() + " is - "
            + second.getAge() + " " + second.getName());

    Contact third = new Contact(34, "Adam", "Male");
    System.out.println("Age of contact " + third.toString() + " is - "
            + third.getAge() + " " + third.getName());

The result will be:

Age of contact Contact@173f7175 is - 25 Yosi
Age of contact Contact@4631c43f is - 22 lisa
Age of contact Contact@6d4b2819 is - 34 Adam

But if I then try to print the first contact again, it will get the values of the last object created. I mean, for this code:

    Contact first = new Contact(25, "Yosi", "Male");
    System.out.println("Age of contact " + first.toString() + " is - "
            + first.getAge() + " " + first.getName());

    Contact second = new Contact(22, "lisa", "Femal");
    System.out.println("Age of contact " + second.toString() + " is - "
            + second.getAge() + " " + second.getName());

    Contact third = new Contact(34, "Adam", "Male");
    System.out.println("Age of contact " + third.toString() + " is - "
            + third.getAge() + " " + third.getName());

    System.out.println("Age of contact " + first.toString() + " is - "
            + first.getAge() + " " + first.getName());

the result will be:

Age of contact Contact@173f7175 is - 25 Yosi
Age of contact Contact@4631c43f is - 22 lisa
Age of contact Contact@6d4b2819 is - 34 Adam
Age of contact Contact@173f7175 is - 34 Adam

I've added up the object string representation so you can see the different objects. I thought I was creating a new object and each object has it's own instance values? could you guys explain to me?

This is the contacts class:

public class Contact {

    private static int age = 0;
    private static String name = "Unknown";
    private static String gender = "Male";

    public Contact(int a, String n, String g) {
        age = a;
        name = n;
        gender = g;

    }

    public Contact() {
    }

    public static int getAge() {

        return age;
    }

    public static String getName() {

        return name;
    }

    public static String getGender() {

        return gender;
    }

    public static void setAge(int a) {

        age = a;

    }

    public static void setName(String n) {

        name = n;
    }

    public static void setGender(String g) {

        gender = g;
    }

}

Please note that if I remove static qualifier I get errors saying "cannot make a static referance to the non static field"

1
  • 2
    Please post the code of your Contact class. Most likely, you have static attributes there. Commented Jun 28, 2012 at 8:11

3 Answers 3

10

Remove the static qualifier from your instance variables and/or methods (age, getAge, name, getName).

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

5 Comments

Thank you this is true I'm using static qualifiers but if I remove them I get an error saying I cannot make a static reference to a non static field
@Yosi199 please post the code of your Contact class. It will be easier for others to help you
Then you are fixing at the wrong end -- change the type of access to non-static instead of making everything downstream static. I bet you are taking Quick Fix as your programming advisor, that's a wrong approach. Quick Fix is here to help people who already know what they want to get it done more quickly.
BTW Have you made sure that your methods getAge, getName don't have the static qualifier?
Thank you all were right and great help. I removed all static refrences fro the get and set methods and instance variables and it works now.
4

This can occur if you mistakenly use a static variable:

class Stat {
  static String name;

  Stat(String n) {
    name = n;
  }
}

In the example class above, all instances will share the same value for name.

Use non-static variables for instance members:

class Stat {
  String name;

  Stat(String n) {
    name = n;
  }
}

Comments

1

Well, Yosi.Static qualifiers tie your fields(and methods) to your class not to your object and each Object of your Class(Contact) share the same value.

Whenever you instantiate it by New, this value gets updated. So when you print fourth time the value it has is what you update it in third time. That'y its printing like that.

As I see you code, if you remove static from everywhere, your code will behave as you desire.

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.