Showing posts with label static. Show all posts
Showing posts with label static. Show all posts

Sunday, 18 January 2015

Java static Variables

If a variable in a class is static, it has only one value, which is shared by all members of the class. You can see this in the example below, where I created a class called Tax with a static variable called VAT:

andrew@UBUNTU:~/Java$ cat Tax.java
public class Tax  
  {
  static double VAT = 10;
  }
andrew@UBUNTU:~/Java$ javac Tax.java
andrew@UBUNTU:~/Java$


Then I wrote a program to use the class. In this program, I created 2 members called member1 and member2 and showed that they both had the same VAT value, i,e, 10. I changed member1.VAT to 11 and showed that member2.VAT changed to 11 too, without a specific assignment. Doing it this way can make your code difficult to follow. An alternative is to modify the static variable by prefixing it with the name of the class. To demonstrate this, I changed Tax.VAT to 12 and checked member1.VAT and member2.VAT to see that they had been altered in the same way:

andrew@UBUNTU:~/Java$ cat Tax_test.java
public class Tax_test
  {
  public static void main(String args[])
    {
    Tax member1 = new Tax();
    System.out.println("member1.VAT = " + member1.VAT);
    Tax member2 = new Tax();
    System.out.println("member2.VAT = " + member2.VAT);
    member1.VAT = 11;
    System.out.println("member1.VAT = " + member1.VAT);
    System.out.println("member2.VAT = " + member2.VAT);
    Tax.VAT = 12;
    System.out.println("Tax.VAT = " + Tax.VAT);
    System.out.println("member1.VAT = " + member1.VAT);
    System.out.println("member2.VAT = " + member2.VAT);
    }
  }
andrew@UBUNTU:~/Java$ javac Tax_test.java
andrew@UBUNTU:~/Java$ java Tax_test
member1.VAT = 10.0
member2.VAT = 10.0
member1.VAT = 11.0
member2.VAT = 11.0
Tax.VAT = 12.0
member1.VAT = 12.0
member2.VAT = 12.0
andrew@UBUNTU:~/Java$

Wednesday, 31 December 2014

Java boolean Variable Default Value

I have read in a few places that the default value of a boolean variable is false so I decided to check for myself. My first attempt is shown in prog58 below:

andrew@UBUNTU:~/Java$ cat prog58.java
public class prog58
{
public static void main (String args[])
  {
  boolean b1;
  System.out.println("b1 is " + b1);
  }
}
andrew@UBUNTU:~/Java$ javac prog58.java
prog58.java:6: variable b1 might not have been initialized
  System.out.println("b1 is " + b1);
                                ^
1 error
andrew@UBUNTU:~/Java$


My second attempt is shown in prog59 below, which does suggest that the default value of a boolean variable is false:

andrew@UBUNTU:~/Java$ cat prog59.java
public class prog59
{
static boolean b1;
public static void main (String args[])
  {
  System.out.println("b1 is " + b1);
  }
}
andrew@UBUNTU:~/Java$ javac prog59.java
andrew@UBUNTU:~/Java$ java prog59
b1 is false
andrew@UBUNTU:~/Java$


The two programs are slightly different. Once I have worked out why one seems to work and the other does not, I will let you know.