Is my explanation correct for static and instance variables?
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
Quote:
(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
Quote:
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.
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.
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).
Quote:
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.