Results 1 to 2 of 2
- 11-01-2010, 05:54 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 1
- Rep Power
- 0
How to call an object from another class?
Hello
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?
- 11-01-2010, 08:14 PM #2
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(); p.setPhoneNr("1234"); Cd cd = new Cd(); cd.setPerson(p); // get phonenumber from cd.person System.out.println("phone number = " + cd.getPerson().getPhoneNr()); } }
what do you think?
Similar Threads
-
Create object of unknown class, based on existing object
By Zack in forum New To JavaReplies: 2Last Post: 06-22-2010, 04:29 AM -
how call from inner class(anonymous or not), a method of parent class?
By lse123 in forum AWT / SwingReplies: 2Last Post: 05-01-2010, 08:59 AM -
add object to ArrayList (object is from extends other class)
By mBull in forum Java AppletsReplies: 3Last Post: 03-15-2010, 08:44 PM -
How can I call abstract class methods from another class
By srinivas2828 in forum New To JavaReplies: 13Last Post: 03-12-2010, 02:33 PM -
How to create object dinamically and call methods.
By Gelembjuk in forum New To JavaReplies: 8Last Post: 10-26-2008, 09:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks