user3241363
user3241363

Reputation: 13

Static Attributes in Java

public class Ride {
   public static String name;
   public static int ticketsRequired;
   public static float heightRequirement;

   public Ride(String name, int ticketsRequired, float heightRequirement) {
      this.name = name;
      this.ticketsRequired = ticketsRequired;
      this.heightRequirement = heightRequirement;
   }

   public static void main(String args[]) { 
      Ride coaster, tosser; 
      coaster = new Ride("Roller Coaster", 6, 4.25f);
      tosser = new Ride("Tummy Tosser", 7, 4.9f);
   }
}

It only takes the value of the last instance which is "tosser" no matter what I call, why is that? Did I mess up with the attributes?

Upvotes: 1

Views: 771

Answers (4)

Dakkaron
Dakkaron

Reputation: 6276

*"But I tried declaring these attributes as non-static, when I use "something.ticketRequired" in another class, it saids "non-static variable ticketsRequired cannot be referenced from a static context""

static means this variable is the same for all instances of this class, so

public static String name;

will be the same for each Object of the class Ride, so coaster.name == tosser.name, because they reference to the same Object. If you assign a new String to it it updates it for all instances.

A static variable can also be accessed by its class name, so you don't even need to have any object to reference to the variable:

System.out.println(Ride.name); //This works too

"non-static variable ticketsRequired cannot be referenced from a static context"

This means that you are trying to access the member variable (=non-static variable) from a static context, so without any Object to reference to it.

If you have a non-static variable you need an Object to reference it:

public String name; //non-static

System.out.println(Ride.name); //doesn't work, since name is not static
System.out.println(coaster.name); //does work, prints "Roller Coaster"
System.out.println(tosser.name);  //does work, prints "Tummy Tosser" 

Upvotes: 0

Chandra Shekhar Goka
Chandra Shekhar Goka

Reputation: 914

static variables are the class variables. Those variables were loaded at the time of loading of the class. Which have the seperate memory location called static pool not in the heap. JVM maintains the copy of the variables for each updation called state of an object. This state will get upted at each instance level those are instance variables. But when come to the static variables only one copy will maintained at the time of class loading. This one copy will get updated. In your example all three variables are created at static pool and loaded at the time of Ride class loaded. And you made 3 updates on that copy. So that final update replaces with previous 2 updates.

Upvotes: 0

Shridhar
Shridhar

Reputation: 11

static variables are class variables. There is only one copy of static variables per class.

You are overwriting the static variables actually.

By removing the static keyword from all the three variables, you will now have instance variables. Every object of the class has its own copy of instance variables.

So the three declarations should be:

public String name;
   public int ticketsRequired;
   public float heightRequirement;

Now each object will have its own copy of these variables and you will get correct output.

Upvotes: 1

rgettman
rgettman

Reputation: 178263

Your class variables are static, which means there's only once copy of each for the entire class. You are overwriting the values in your constructor with each new object you create.

To have different values for each object, they need to be not static:

public String name;
public int ticketsRequired;
public float heightRequirement;

The fact that they're public means that they are accessible from any other class.

But that's also a sign that your class isn't properly encapsulated. You can make your fields accessible, even if they're private, but defining public accessor methods, e.g. getName().

Example, with one of the variables:

private String name;

and

public String getName() { return name; }

Upvotes: 4

Related Questions