Results 1 to 3 of 3
  1. #1
    jackandjill is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Default Is my explanation correct for static and instance variables?

    Java Code:
    public class Coin {
    2 private static int numOfObjects = 0; // to keep track of number of objects created
    3 private int denomination; // to keep track of the denomination of coin
    4
    5 public Coin() {
    6 numOfObjects++;
    7 }
    8
    9 public int getNumOfObjects() {
    10 return numOfObjects;
    11 }
    12
    13 public void setDenomination(int d) {
    14 denomination = d;
    15 }
    16 public int getDenomination() {
    17 return denomination;
    18 }
    19 } // end Coin class
    1 public class CoinTester{
    2 public static void main(String[] args) {
    3 int x = 3, y = 2;
    4 System.out.println("Number of coins = " + Coin.getNumOfObjects());
    5 Coin coin1 = new Coin();
    6 Coin coin2 = coin1;
    7 Coin coin3 = new Coin();
    8 System.out.println("Number of coins = " + coin2.getNumOfObjects());
    9 coin1.setDenomination(x);
    10 y = x;
    11 coin3.setDenomination(y);
    12 System.out.println("Denomination of coin1 = " +
    13 coin1.getDenomination());
    14 System.out.println("Denomination of coin2 = " +
    15 coin2.getDenomination());
    16 System.out.println("Denomination of coin3 = " +
    17 coin3.getDenomination());
    18 System.out.println("coin1 == coin2 is " + (coin1==coin2));
    19 System.out.println("coin1 == coin3 is " + (coin1==coin3));
    20 System.out.println("coin3 == coin2 is " + (coin3==coin2));
    21 }
    22 } // end CoinTester class

    (a) Refer to the class Coin. The variable numOfObjects is declared as a static variable while the variable denomination is declared as an instance variable. Explain the difference between these two types of variables.
    so the answer that i gave is this



    a) The difference between static and instance variables are
    Static variables are defined at Class level, so which means there values are also initialized and loaded when the class has been called
    Instance variables are defined at instance level, and there values differ from one initiated place to another unlike Static variables where the values are literally static and cannot be changed unless changed in the class that has declared the static variable.
    Last edited by pbrockway2; 09-23-2012 at 05:26 AM. Reason: tags changed

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Is my explanation correct for static and instance variables?

    Because of a bug in the forum software long lines in "code" tags are often unreadable, so I've changed the question and answer to use "quote" tags.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Is my explanation correct for static and instance variables?

    In fact static variables can be changed from anywhere they are visible. In the case of the code you posted numOfObjects cannot be changed outside the Coin class because it is private not because it is static.

    So "static" means "having the same value for all instances" while instance variables have a different value for each instance. (as you said).

    there values are also initialized and loaded when the class has been called
    This is more or less true. But we usually don't say that classes are called. So it might be better to say, of static variables, "their values are initialized when the class is first used."

    ---

    Notice a slight oddity in the code. Methods as well as variables can be static and getNumOfObjects() would, more naturally, be declared as a static method since it only makes use of static variables.

Similar Threads

  1. Instance variables
    By lala in forum New To Java
    Replies: 1
    Last Post: 01-26-2011, 04:38 PM
  2. Replies: 6
    Last Post: 02-15-2010, 04:31 AM
  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

Tags for this Thread

Posting Permissions

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