Results 1 to 2 of 2
Thread: Instance variables
- 01-26-2011, 04:33 PM #1Member
- Join Date
- Oct 2010
- Rep Power
I think I'm making a concept harder than it really is again. No program written yet, just going through some concepts.
public class Clock
private int hr; // store the hours
private int min; // store the minutes
private int sec; // store the seconds
"The non-static data members of a class are called instance variables. Therefore, the variables hr, min, and sec are the instance variables of the class Clock."
Definition from :Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)
Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words)...
I don't get it.The definition "unique to each instance of a class"?
- 01-26-2011, 04:38 PM #2
What happened when you threw together a simple example to play with?
Say you have some class called ExampleObject. ExampleObject has a non-static field in it called someVariable.
You can then have:
ExampleObject obj1 = new ExampleObject(); ExampleObject obj2 = new ExampleObject(); obj1.someVariable = 1; obj2.someVariable = 2;
That's not true with static variables. If you change the static variable of a class, you change it for every instance of that class.
I really recommend you throw together a basic example and test it out yourself.
- By Addez in forum New To JavaReplies: 2Last Post: 04-04-2010, 03:36 PM
- By CyJackX in forum New To JavaReplies: 12Last Post: 03-16-2010, 06:45 PM
- By sandeshforu in forum New To JavaReplies: 3Last Post: 09-09-2009, 05:48 PM
- By ravian in forum New To JavaReplies: 7Last Post: 03-01-2009, 10:09 PM
- By gabri in forum Advanced JavaReplies: 12Last Post: 09-30-2008, 06:30 PM