Results 1 to 7 of 7
- 11-04-2011, 04:27 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
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?
-
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?
- 11-04-2011, 04:54 AM #3
Re: What to do with object 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.
If you have a reference to an object then you would call the appropriate method on that reference to get the value you want.How do I use it to get the integers
- 11-04-2011, 05:28 AM #4
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
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 05:34 AM.
- 11-04-2011, 05:36 AM #5
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.
- 11-04-2011, 05:40 AM #6
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
Re: What to do with object reference?
ok I will do so :)
- 11-04-2011, 05:58 AM #7
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
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
-
Reference object
By clj89 in forum New To JavaReplies: 5Last Post: 10-22-2011, 11:32 PM -
object and reference
By aizen92 in forum New To JavaReplies: 11Last Post: 04-01-2011, 08:39 PM -
Object and reference
By katie in forum New To JavaReplies: 2Last Post: 10-19-2009, 03:45 PM -
Very new to Java, how to reference an object
By tornado in forum New To JavaReplies: 3Last Post: 12-05-2008, 12:51 AM -
Getting the Object Reference Name
By Deathmonger in forum New To JavaReplies: 2Last Post: 03-12-2008, 02:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks