Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Fubarable

Thread: How do I link a class into another class?

  1. #1
    ekay90 is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default How do I link a class into another class?

    I'm using Eclipse. I have a tab for Class1 and a separate tab for Class2.

    In Class1, I want to say something like, "if (num != 2), then implement the code in Class2"

    How do I do this?

    Thank you.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: How do I link a class into another class?

    implement the code in Class2
    By implement I assume you mean you want to execute a method in Class2.
    The way to execute code in another class is to get a reference to an instance of the class and use it:
    Java Code:
    refToClass2.methodInClass2();  // call a method in Class2
    One way to get a reference to another class use the new statement:
    Java Code:
      Class2 refToClass2 = new Class2(<The args here>);
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ekay90 is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: How do I link a class into another class?

    Quote Originally Posted by Norm View Post
    Java Code:
     Class2 refToClass2 = new Class2(<The args here>);
    Regarding this code, what is the refToClass2? What is an example of a reference to Class 2?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: How do I link a class into another class?

    Quote Originally Posted by ekay90 View Post
    Regarding this code, what is the refToClass2? What is an example of a reference to Class 2?
    That is. ^^

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    ekay90 is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: How do I link a class into another class?

    So in that case, refToClass2 is just like the variable name?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: How do I link a class into another class?

    refToClass2 is just like the variable name?
    refToClass2 is the variable name.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: How do I link a class into another class?

    i dont want to hijack this thread but i actually have a question about the answer given,

    i thought in the example Class2 refToClass2 = new Class2 , this creats a Class2 object right? so refToClass2 is an object name? or is it still considered a variable that refrences the object?

    once again sorry i kinda hijacked the thread with my own question

    Russ

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

    Default Re: How do I link a class into another class?

    refToClass2 is a Class2 variable that after refers to a Class2 object after the constructor call. You could assign it to null on the next line, and it is still a Class2 variable, but just one that does not refer to any object.
    DarrylBurke likes this.

  9. #9
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: How do I link a class into another class?

    thank you for the clarification, i was confused about that after i read the post. once again sorry for posting this on someone elses thread.

    Russ

Similar Threads

  1. Replies: 1
    Last Post: 12-13-2012, 03:06 AM
  2. Replies: 1
    Last Post: 11-29-2012, 09:46 AM
  3. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  4. how to send a link through action class in struts
    By Kadiyala in forum New To Java
    Replies: 0
    Last Post: 02-17-2011, 07:44 AM
  5. How to create main class link to another two class?
    By pearllymary78 in forum New To Java
    Replies: 6
    Last Post: 07-16-2008, 11:02 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
  •