Results 1 to 4 of 4
  1. #1
    skiabox is offline Member
    Join Date
    Jul 2011
    Posts
    17
    Rep Power
    0

    Default 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:

    Java 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.

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

    Default Re: A couple of questions about static classes and static members

    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.

  3. #3
    skiabox is offline Member
    Join Date
    Jul 2011
    Posts
    17
    Rep Power
    0

    Default 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. '

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default 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:

    Java 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

    Java 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.

Similar Threads

  1. who initializes static members?
    By munishvit in forum New To Java
    Replies: 10
    Last Post: 12-27-2010, 05:43 AM
  2. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  3. static vs. non-static nested classes
    By rinke in forum Advanced Java
    Replies: 8
    Last Post: 06-30-2009, 07:15 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 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
  •