A couple of questions about static classes and static members
I am reading a book and there is a listing in it with this code in it:
Code:
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
public class AccountWithSyncUsingLock {
private static Account account = new Account();
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
// Create and launch 100 threads
for (int i = 0; i < 100; i++) {
executor.execute(new AddAPennyTask());
}
executor.shutdown();
// Wait until all tasks are finished
while (!executor.isTerminated()) {
}
System.out.println("What is balance ? " + account.getBalance());
}
// A thread for adding a penny to the account
public static class AddAPennyTask implements Runnable {
public void run() {
account.deposit(1);
}
}
// An inner class for account
public static class Account {
private static Lock lock = new ReentrantLock(); // Create a lock
private int balance = 0;
public int getBalance() {
return balance;
}
public void deposit(int amount) {
lock.lock(); // Acquire the lock
try {
int newBalance = balance + amount;
// This delay is deliberately added to magnify the
// data-corruption problem and make it easy to see.
Thread.sleep(5);
balance = newBalance;
}
catch (InterruptedException ex) {
}
finally {
lock.unlock(); // Release the lock
}
}
}
}
I can understand the proper use of static nested classes by reading this article : http://onjava.com/onjava/excerpt/Har...06/index2.html
What I cannot understand is why the author is using static keyword only for variables that refer to an object and not for primitive variables.
Thank you.
Re: A couple of questions about static classes and static members
Quote:
What I cannot understand is why the author is using static keyword only for variables that refer to an object and not for primitive variables.
Which variable or variables do you think should be declared differently?
-----
I don't think the primitive vs reference type distinction is necessarily what is driving the author's choice.
Re: A couple of questions about static classes and static members
I get it know.I've read this article here : » Blog Archive » Java Classes and Objects | Java Beginner
The phrase that solved all the above questions is this : 'Static variables can be accessed even though no objects of that class exist. '
Re: A couple of questions about static classes and static members
What you should understand about static members in a class is that they are the same for every instance of that class.
They can be used on primitives too. Take this example as described in the Java Tutorials:
Code:
class Bicycle {
public static int numOfBicycles = 0;
public int bicycleId;
//constructor
Bicycle() {
bicycleId = ++numOfBicycles;
}
//getters
public int getId() { return bicycleId; }
public static int getNumOfBicycles() { return numOfBicycles; }
}
Create a test class to test it out
Code:
...
public static void main(String[] args) {
System.out.println(Bicycle.getNumOfBicycles()); //output = 0
Bicycle myBike = new Bicycle();
System.out.println(Bicycle.getNumOfBicycles()); //output = 1
Bicycle hisBike = new Bicycle();
System.out.println(Bicycle.getNumOfBicycles()); //output = 2
System.out.printf("myBike id=%d%n", myBike.getId()); //output = myBike id=1
System.out.printf("hisBike id=%d%n", hisBike.getId()); //output = hisBike id=2
}
...
In each bicycle instance, the non-static bicycleId field is different, but the static field numOfBicycles is the same.