Zack Gurien
Zack Gurien

Reputation: 11

Constructor confusion Java

Hi this might seem like a really stupid question, but I recently got into java and am teaching myself about constructors.

public class creatures {
    private static String name;
    private static int age;
    private static String type;

    public creatures( String name, int age, String type) {
        this.name = name;
        this.age = age;
        this.type = type;
        System.out.println("The creature's name is " + name + " \nThe creatures age is"  + age + " \nThe creatures type is " + type);
    } 

    public  static void main(String [] args) {
        creatures newcreature = new creatures("Zack", 100, "alien");
        creatures newcreature1 = new creatures("Jonny", 500, "vampire");
        creatures newcreature2 = new creatures("Dick", 4, "witch");
        System.out.println(newcreature.name);
    }
}

So in the system.out.println in my main method, after the constructors are printed, I want to print the name "Zack" by referencing the name of my newcreature constructor, but it just prints the name "Dick" from the last constructor that I made. How do I distinguish between these constructors that are in the same class? Again sorry if this is a stupid question.

Upvotes: 1

Views: 79

Answers (4)

ddarellis
ddarellis

Reputation: 3960

The problem is with the static keyword at your variables.

Read this: enter link description here

static variable will get the memory only once, if any object changes the value of the static variable, it will retain its value.

Upvotes: 2

Touchstone
Touchstone

Reputation: 5962

All data members of class are static that's why every instance share the same member. When ever you are creating new instance of creatures the constructor is just overriding the old values with the new values.

In your code:

private static String name; 
private static int age;
private static String type;

are shared among creature, creature1, creature2.

Remove static key words.

public class creatures {
 private String name;
 private int age;
 private String type;

 public creatures(String name, int age, String type) {
    this.name = name;
    this.age = age;
    this.type = type;
    System.out.println("The creature's name is " + name
            + " \nThe creatures age is" + age + " \nThe creatures type is "
            + type);
 }

 public static void main(String[] args) {
    creatures newcreature = new creatures("Zack", 100, "alien");
    creatures newcreature1 = new creatures("Jonny", 500, "vampire");
    creatures newcreature2 = new creatures("Dick", 4, "witch");
    System.out.println(newcreature.name);
 }

}

Upvotes: 0

singhakash
singhakash

Reputation: 7919

Because your name field is static so it shares a common memory.So if you try to access it with refrencing it with different object it will give the same output.

Since you last changed the value new creatures("Dick", 4, "witch"); with Dick it will be changeg to it.

so remove the static keyword to get the desired o/p

public class creatures {
    private String name;
    private int age;
    private String type;

    public creatures( String name, int age, String type) {
        this.name = name;
        this.age = age;
        this.type = type;
        System.out.println("The creature's name is " + name + " \nThe creatures age is"  + age + " \nThe creatures type is " + type);
    } 

    public  static void main(String [] args) {
        creatures newcreature = new creatures("Zack", 100, "alien");
        creatures newcreature1 = new creatures("Jonny", 500, "vampire");
        creatures newcreature2 = new creatures("Dick", 4, "witch");
        System.out.println(newcreature.name);
    }
}

Output

Zack

Upvotes: 0

lodo
lodo

Reputation: 2383

Your fields name, age and type are static. This means that they are shared by all your creatures. So you can't say "the name of this creature is ...", because a creature has no name in your code. As it is written, you can only say "the creature class has this name ...", that in Java is written creatures.name=....

So, you need to remove that static modifier from your fields.

Upvotes: 0

Related Questions