Results 1 to 4 of 4
  1. #1
    tornado is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default Very new to Java, how to reference an object

    Hi,

    Let's say I have 2 classes called Bob and Mary, class Bob is shown below.

    public class Bob {
    private String name;
    private int age;

    public Bob(String name, int age)
    {
    name = this.name;
    age = this.age;
    }

    public void setName(String newName){
    name = newName;
    }

    //Main Method Below
    b1 = new Bob(testname, 22);
    }

    OK now in class Mary how can I access b1 and bob's method. So in class Mary I want to do:
    b1.setName("testnewname");
    By the way I know I could do Bob b1 = new Bob(name, 22); in class Mary instead of creating it in Bob, but is there any way to solve it how I want.
    So in essence, how do I pass b1 onto class Mary, so I can reference it from class Mary.

    Thanks to any help it's appreciated.
    Last edited by tornado; 12-05-2008 at 01:05 AM.

  2. #2
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    First off, you've made your constructor in Bob incorrectly. It should be:
    this.name = name
    not:
    name = this.name

    Regarding you main question, I don't understand why you want to do that. Which class will have the main method? Bob? So in the main method in Bob, you want to create an object of Bob... I suppose if you made your Bob object static, i.e. static Bob b1 = new Bob(); you could access it from Mary with Bob.b1 but I still fail to see why you want to do this in the first place.

  3. #3
    tornado is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    Thanks, you are infact right there was no need for me to access it from the other class, i'm sorry i'm a bit too quick to post sometimes!

    Thanks for the guidence.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Head spinning ...

    Let's see ...
    Your first question seems to be:
    OK now in class Mary how can I access b1 and bob's method. So in class Mary I want to do:
    b1.setName("testnewname");
    Which is a valid question, but later on you state:
    By the way I know I could do Bob b1 = new Bob(name, 22); ...
    Therefore I assume that you know that to access Bob's methods from the Mary class you have to:
    Java Code:
    Bob b1 = new Bob(name, 22);
    b1.setName("testnewname");
    Is you're question how to access Bob's method from Bob's main method? If so, why would Bob have a main method?

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Object and reference
    By katie in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 04:45 PM
  2. Replies: 3
    Last Post: 04-17-2009, 06:07 AM
  3. A Java reference source for a quick look
    By tapies in forum New To Java
    Replies: 6
    Last Post: 10-03-2008, 07:01 PM
  4. Object reference to number
    By dayneo in forum New To Java
    Replies: 4
    Last Post: 05-27-2008, 02:23 PM
  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
  •