<Java> Passing by reference problem

Discussion in 'OT Technology' started by Disguy, Oct 21, 2008.

  1. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,061
    Likes Received:
    0
    For my project I have to code a pet. It has to eat, sleep and speak. Simple right.

    My problem lies in making the dog go from sleep to awake. I'm calling the changeState method in the main method but doing so gives an error "cannot reference a non-static method in a static method".

    OK, simple so I change changeState to a static method and then I get the same error message when refering to the setSleeping method.

    :noes:

    I don't get how I can change the boolean value of sleep!

    I'm going to look through my text some more but I was hoping somebody might be able to point me in the right direction.

    I sure it lies in the fact that I'm not entirely sure how to use getters and setters correctly.



    Code:
    package pet;
    
    
    public class CyberPet {
    
        String name;
        Boolean sleeping;
    
        public static void main(String[] args) {
            
            CyberPet x = new CyberPet();
            
            x.sleeping=false;
            
            System.out.print(x.sleeping);
            if (x.sleeping==true)
                System.out.println(" means I'm asleep.");
            else System.out.println(" means I'm awake.");
            
            
            changeState(false);
            
               }
            
            
        }
           public void changeState(boolean response){
           
            if (response==false){
                setSleeping(true);
            }
            else if (response==true){
                setSleeping(false);
            }        
            
        }
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Boolean getSleeping() {
            return sleeping;
        }
    
        public void setSleeping(Boolean sleeping) {
            this.sleeping = sleeping;
        }
        
     
  2. quzer

    quzer New Member

    Joined:
    Nov 9, 2003
    Messages:
    206
    Likes Received:
    0
    Code:
    x.changeState(false);
    
    You gotta reference the object whose method you want to access. When you did x.sleeping=false it worked, you're referencing the object 'x'. If you tried to do 'sleeping=false' the program would not know which object's sleeping attribute we're changing, in a static context 'this' doesn't reference anything.
     
  3. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    OK, here's a quick run-down on accessors and mutators (gets and sets). They're based on the premise that the data members in your class are set to private (which yours aren't in this case). If you set your data members to private, then the following line <code> x.sleeping=true</code> is not allowed because access to private data members is denied. Instead, any time you need the value of sleeping, you want to use the getSleeping() method. Similarly, whenever you want to set the value of a data member, you want to use the set() methods you've written. The gets and sets you write expose an interface for other programmers to use your class. Take the pet's name for example. What if you wanted to limit the pet name so that no numerical characters could appear in the pet name? What you would do is write some code in the setName() method that would check the name that was passed in and verify that it doesn't contain any illegal characters. Without private data members and gets/sets, you have no control over what kind of values get put into your data members.
     
  4. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,061
    Likes Received:
    0
    thanks guys I'm going to try that out. cool pts for all!
     
  5. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    wtf, you can't do x.sleeping = false.... x.setSleep(false)
     
  6. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    Sure you can, if sleeping is a public data member.
     
  7. CodeX

    CodeX Guest

    "getters" and "setters" that literally only assign or return a value are pretty gay anyways... even in high level OOP I tend to avoid such ridiculous restrictions, just make the damn thing public
     
  8. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,061
    Likes Received:
    0
    Thanks again thats all I needed to do. everything works fine now!:bigthumb:
     
  9. quzer

    quzer New Member

    Joined:
    Nov 9, 2003
    Messages:
    206
    Likes Received:
    0
    np, i also just noticed that the explanation i gave probably doesn't make sense lol
     
  10. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY

    no


    You mean this.sleeping=false
     
  11. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    not from the main method, you have to do it through the object. you cannot use non-static variables from a static method.
     
  12. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    No.

    From the static main method, the 'this' reference does not exist. x does exist though. So you can do x.sleeping = false;
     

Share This Page