Results 1 to 4 of 4
  1. #1
    DayKema is offline Member
    Join Date
    Jul 2014
    Posts
    23
    Rep Power
    0

    Default Little problem? Simple?

    Trying to understand static variables and well other stuff.

    anyways in the console I get this:

    Java Code:
    There are 2, of dogs in the club
    There are 2, of dogs in the club
    There are 2, of dogs in the club
    WOOF! WOOF!
    Bark! 
    Yiiip! Yip!

    Here is my code.

    Class Test
    Java Code:
    
    package test;
    
    /**
     *
     * @author DayKema
     */
    public class Test {
    
    int size;
    String name;
    public static int number = 0;
    
    public Test(int NB){
        number = NB;
        number++;
        System.out.printf("There are %s, of dogs in the club\n", number);
    }
    
    public void bark(){
        if(size > 60){
            System.out.println("WOOF! WOOF!");
        } else if(size > 30) {
                    System.out.println("Bark! ");
        } else if(size > 10) {
                System.out.println("Yiiip! Yip!");
        }
        
    }
    
        }

    Now here is my TestDrive Class:S
    Java Code:
    public class TestDrive {
        
        public static void main(String[] args){
            
            
            Test [] dogs = new Test[3];
            
            dogs[0] = new Test(1);
            dogs[1] = new Test(1);
            dogs[2] = new Test(1);
            
            
            dogs[0].size = 70;
            
            dogs[1].size = 45;
            dogs[2].size = 18;
            
            dogs[0].name = "Bob";
            int x = 0;
            while(x < 3){
               dogs[x].bark();
               x++;
            }
            
            
        }
        
    }
    Why does the console print out 2, for static variable and not 3. There are 3 dog objects

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Little problem? Simple?

    Quote Originally Posted by DayKema View Post
    Why does the console print out 2, for static variable and not 3. There are 3 dog objects
    Because you create the objects with a value of 1 in the constructor. Also, that has nothing to do
    with static. You just keep resetting the static value to 1 and then incrementing it before printing it out.

    To better understand static values, check out Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DayKema is offline Member
    Join Date
    Jul 2014
    Posts
    23
    Rep Power
    0

    Default Re: Little problem? Simple?

    Quote Originally Posted by jim829 View Post
    Because you create the objects with a value of 1 in the constructor. Also, that has nothing to do
    with static. You just keep resetting the static value to 1 and then incrementing it before printing it out.

    To better understand static values, check out Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Regards,
    Jim
    Okay thank you.

    So now how would I go about incrementing it to 3?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Little problem? Simple?

    Quote Originally Posted by DayKema View Post
    So now how would I go about incrementing it to 3?
    Because it's static, anything you do to it will be reflected in the class and not the individual instances. It is a class variable. So
    remove the static modifier from your variable to make it an instance variable. Then simply pass 0,1,2 to the constructor calls in
    succession.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Problem with a Simple Histogram Problem
    By kathmandu in forum New To Java
    Replies: 12
    Last Post: 06-25-2013, 02:19 AM
  2. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 06:12 AM
  3. Simple problem
    By melovehockey in forum New To Java
    Replies: 8
    Last Post: 12-29-2010, 02:43 AM
  4. I have a simple problem
    By o0oNorao0o in forum JCreator
    Replies: 20
    Last Post: 12-25-2009, 09:32 PM
  5. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 AM

Posting Permissions

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