Results 1 to 7 of 7

Thread: Static Fields Confusion

  1. #1

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Question Static Fields Confusion

    I'm still fuzzy on the topic of static variables in OOP. Correct me if I'm wrong or add to it but if you declare a static variable in a class, that variable is visible only to that class no matter what? I don't really understand. Any help would be great!
    Only those who try will become.

    Find me on identi.ca

    Twitter @gfmartin05

    Linux Wrap

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Static Fields Confusion

    A static var (attribute) in Java is a value shared by all instances. if you change it form an instance of the class it'll change for all other instances also.

    A static var (within a method) value is preserved after exiting the method unlike regular vars
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Re: Static Fields Confusion

    So if I instantiate a class called Book three times and I have a static variable called bookISBN and I change the value of that variables through a method, that value will then change for all three instances right? And static variables within a method would basically be just like constants right?

    So then a static method would be a method shared by all instances of that class right?
    Only those who try will become.

    Find me on identi.ca

    Twitter @gfmartin05

    Linux Wrap

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Static Fields Confusion

    Right except that a static value in a method can be modified but doesn't lose value upon exiting method
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    New Member Santa Clause's Avatar
    Join Date
    Dec 2003
    Location
    North Pole. Lapland when I visit my Wife.
    Posts
    10

    Re: Static Fields Confusion

    In OOP terms a static member or method does not require an instance of that object. To draw a real world comparison; think of a plastic mould used to create chocolate Santa's. This mould is the class/template used to create many instances of Chocolate Santa's.

    On the mould however, there is a batch number. Every time you create new Santa, the batch number appears on the bottom and it does not change, however one can still read the batch number without actually creating the Santa, simply by looking at it.

    In OOP terms you can access a static member or method using the class name:
    Code:
    class ChocolateSanta
    {
        private static int BATCH_NUMBER=1234;
        private static int productionCount = 0;
    
        private boolean melted = false;
    
        public ChocolateSanta() {
            productionCount++;
        }
    
        public int static getBatchNumber() {
            return BATCH_NUMBER;
        }
    
        public static int getProductionCount() {
            return productionCount;
       }
    
        public void melt()
        {
            melted = true;
        }
    
        public boolean getMelted()
        {
            return melted;
        }
    }
    
    public class StantaFactory
    {
        public static void main(String[] args)
        {
            System.out.println(ChocolateSanta.getProductionCount() + " Santa's made");
    
            ChocolateSanta yumYum = new ChocolateSanta;
            
            System.out.println(ChocolateSanta.getProductionCount() + " Santa's made");
    
            yumYum.melt();
            
            if (yumYum.getmelted()) {
                System.out.println("Sorry your chocolate santa has melted");
            }
        }
    }
    You can see that another static variable is used to keep track of the production count. And, static methods are used to access the static variables. Remember that one can only access static class variables and methods inside a static method.

    You can also declare an entire class static. If you do this, you can never create an instance of it. This can be useful in a situation where you may want to create a library of global methods for example.
    I am the real Santa Clause

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Static Fields Confusion

    You do know Santa is a myth, don't you?
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: Static Fields Confusion

    Quote Originally Posted by ComputerJy
    You do know Santa is a myth, don't you?
    Santa, is that you?

Posting Permissions

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



Click Here to Expand Forum to Full Width