Results 1 to 2 of 2
  1. #1
    narty is offline Member
    Join Date
    Nov 2010
    Rep Power

    Default How to call an object from another class?


    In my java homework i have to have a field, from a class called Cd point to an object from another class (Person). Now i have tried mighty hard but i just cannot make the final connection, i understand that the class Cd will have a field of type Person which would mean it holds information about an object from that class. i think?

    So now my lecturer described it to me with a telephone number example, i could go round the class and get everyones number or i could just get them to write it on a piece of paper and request the paper, which i gathered resembles an object i.e it has the information stored in its fields

    Now my issue is how do i call that object from a method in another class i think im going down the right tracks but i seem to be getting more and more confused please can somebody steer me in the right direction?

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Zurich, Switzerland
    Rep Power


    hello, i don't know if i got your reguirements by 100% but i have implemented an example:

    Java Code:
    class Person {
    	String phoneNr;
    	public void setPhoneNr(String phoneNr) {
    		this.phoneNr = phoneNr;
    	public String getPhoneNr() {
    		return this.phoneNr;
    public class Cd {
    	Person person;
    	public void setPerson(Person p) {
    		this.person = p;
    	public Person getPerson() {
    		return this.person;
    	 * @param args
    	public static void main(String[] args) {
    		Person p = new Person();
    		Cd cd = new Cd();
    		// get phonenumber from cd.person
    		System.out.println("phone number = " + cd.getPerson().getPhoneNr());

    what do you think?

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2010, 05:29 AM
  2. Replies: 2
    Last Post: 05-01-2010, 09:59 AM
  3. Replies: 3
    Last Post: 03-15-2010, 09:44 PM
  4. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 03:33 PM
  5. How to create object dinamically and call methods.
    By Gelembjuk in forum New To Java
    Replies: 8
    Last Post: 10-26-2008, 10:28 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