Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default What to do with object reference?

    Hi, I just started programming in Java. So if I understand correctly, objects are always pass-by-reference. Now I am storing 3 integers in an object called Beepers. (int count, street, avenue;) So I got two Beepers objects stored in the array and I used get(index) to get a reference to two different objects. I thought I would get an integer back, but nooooooooooooo. Of course it is not that simple. So now what do I do with this "reference". How do I use it to get the integers I stored in this object?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: What to do with object reference?

    Stored in an array? or an ArrayList since it is an ArrayList that has a get() method?

    If you're getting the Beeper object from the ArrayList, then what you get back is a Beeper object. Otherwise perhaps you want to supply us with more necessary details?

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: What to do with object reference?

    Quote Originally Posted by kyle_maddisson View Post
    objects are always pass-by-reference.
    No. Wrong. Incorrect. Java is ALWAYS pass-by-value. Primitives are PBV. Object references are PBV. There is a distinct difference and you should get that clear in your head. Google "first cup of java" for a good explanation.
    How do I use it to get the integers
    If you have a reference to an object then you would call the appropriate method on that reference to get the value you want.

  4. #4
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default Re: What to do with object reference?

    Well I use the get() method and I call a getStreet() method which simply returns the variable street, however I get 0 back, which is not a value I stored. I don't get this, man!
    Last edited by kyle_maddisson; 11-04-2011 at 06:34 AM.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: What to do with object reference?

    I know this is hard to believe but we don't read minds. We cannot see your computer and rely on you posting enough information so that we can identify the problem. Try posting some code and sample output. If your code is large then create a SSCCE.

  6. #6
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default Re: What to do with object reference?

    ok I will do so :)

  7. #7
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default Re: What to do with object reference?

    Ok first of all, what I am trying to accomplish is thus:

    I have created a class called UrRobot, which is to model the Karel++ UrRobot. Now I need to create a class to hold all the information needed to interact with objects in a karel world, namely walls and beepers. I have managed to build a sub class called Beepers which stores which intersection (st, ave) and how many beepers are on that intersection (count). I also managed to put two Beeper objects in an ArrayList called beeperList. Now i am just trying to print the values stored in the ArrayList just to make sure I have to right information to continue the tests and call the Beeper object's methods. Right now my code is a mess, it compiles but to you it may seem confusing due to the fact that I have print statements put there to help me follow the flow of the program, and to help me to see the behavior of the program.

    Anyway here is the program that uses the classes, BasicUrRobotTest:


    public class BasicUrRobotTest{
    public static void main(String args[]){

    UrRobot karel = new UrRobot(1,1,3,0);
    //karel.LinkTest();
    System.out.println("Initial position:");
    System.out.println("*****BasicUrRobotTest calls display()******");
    karel.display();

    System.out.println("*****BasicUrRobotTest calls pickBeeper()******");
    karel.pickBeeper();
    System.out.println("*****BasicUrRobotTest calls pickBeeper()******");
    karel.pickBeeper();
    //karel.LinkTest();
    //karel.w.LinkTest();
    //karel.w.beeperList.get(0).LinkTest();
    //karel.w.beeperList.get(1).getStreet();
    System.out.println("so now this is what i get back for street "+karel.w.beeperList.get(0).getStreet());
    }

    public BasicUrRobotTest(){
    System.out.println("*** UrRobot Constructor ***");
    }
    }


    and here is the World class that has the Beepers class and it's methods in it:

    import java.util.ArrayList;

    public class MyWorld{

    /************** declare variables ****************/
    static ArrayList<Beepers> beeperList = new ArrayList<Beepers>();

    /*********** Constructor ************/
    public MyWorld(){ /* add walls and beepers here */
    System.out.println("***MyWorld Contructor***");
    beeperList.add(new Beepers (100,11,31));
    beeperList.add(new Beepers (111,17,37));
    }

    /********** Inner Class "Beepers" **************/
    public class Beepers{

    /******** declare variables *************/
    private int street, avenue, count;


    public Beepers(int s, int a, int c){ // Beepers class constructor
    System.out.println("*** Beepers Constructor***");
    int street=s;
    int avenue=a;
    int count=c;
    System.out.println("***Beepers Constructor was passed "+s +" " +a +" "+ c);

    }
    /************Beeper Class Methods ********************************/

    public int getStreet(){
    System.out.println("*** getStreet() ***");
    System.out.println("Street is"+ beeperList.get(0).street);
    return street;
    }
    }


    hope this helps. I didn't include UrRobot since that one is fine.

Similar Threads

  1. Reference object
    By clj89 in forum New To Java
    Replies: 5
    Last Post: 10-23-2011, 12:32 AM
  2. object and reference
    By aizen92 in forum New To Java
    Replies: 11
    Last Post: 04-01-2011, 09:39 PM
  3. Object and reference
    By katie in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 04:45 PM
  4. Very new to Java, how to reference an object
    By tornado in forum New To Java
    Replies: 3
    Last Post: 12-05-2008, 01:51 AM
  5. Getting the Object Reference Name
    By Deathmonger in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 03:51 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
  •