Results 1 to 2 of 2
  1. #1
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Instance variables

    Hi.
    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"?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default

    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:

    Java Code:
    ExampleObject obj1 = new ExampleObject();
    ExampleObject obj2 = new ExampleObject();
    obj1.someVariable = 1;
    obj2.someVariable = 2;
    So each instance of ExampleObject has someVariable, but the value of that variable depends on which instance you're talking about.

    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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Find instance that owns instance?
    By Addez in forum New To Java
    Replies: 2
    Last Post: 04-04-2010, 03:36 PM
  2. Replies: 12
    Last Post: 03-16-2010, 06:45 PM
  3. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  4. accessing instance variables from static methods
    By ravian in forum New To Java
    Replies: 7
    Last Post: 03-01-2009, 10:09 PM
  5. static are instance variables
    By gabri in forum Advanced Java
    Replies: 12
    Last Post: 09-30-2008, 06:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •