Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: calling objects in a differnt class

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default calling objects in a differnt class

    Hey evyone,

    I have three classes

    I made objects out of classA in Class B...
    For some refernce can i use those objects in class C...

    Thanks


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: calling objects in a differnt class

    Yes, as long as C has a reference to them.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: calling objects in a differnt class

    how do we refernce objects??

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: calling objects in a differnt class

    AnObject anObjReference = new AnObject();

    anObjReference is a reference to an object.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: calling objects in a differnt class

    i have object reference

    ClassA[] obj=new classA[10]; in Class B


    can i use 'obj[0]' in classC

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: calling objects in a differnt class

    How will class C get the reference?
    Does class C have a reference to the class B object?
    If it does, it can call a method in class B to get to the obj variable. For example:
    have a method in Class B:
    public ClassA getRef() {
    return obj[0];
    }

    Then in class C, if there is a ref to class B: refClassB
    ClassA objZero = refClassB.getRef(); // get obj[0] from class B

  7. #7
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: calling objects in a differnt class

    Norm ,
    thanks


    I have one issue
    i have three classes

    public class dd {
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    node[] nodeobj=new node[1];
    nodeobj[0]=new node(2);


    nodeobj[0].xy();
    System.out.println(nodeobj[0].obj[0].y);

    }

    }

    public class node {
    int x;

    public static di[] obj=new di[1];

    obj[0]=new di(22);


    public node(int p)
    {
    x=p;
    }

    public void xy()
    {


    }


    }

    public class di {

    int y;

    public di(int f)
    {
    y=f;
    }
    }


    why can not i print nodeobj[0].obj[0].y

    when i made a single object it printed nodeobj[0].obj.y

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: calling objects in a differnt class

    Please copy and paste here the output from when you execute the program.
    Do you get errors? Copy and paste the full text here.

    Please use code tags around your code when posting. See: BB Code List - Java Forums

  9. #9
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: calling objects in a differnt class

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Syntax error on token ";", { expected after this token
    Syntax error, insert "}" to complete MethodBody
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "void", @ expected
    Syntax error, insert "}" to complete ClassBody

    at node.<init>(node.java:7)
    at dd.main(dd.java:11)

  10. #10
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: calling objects in a differnt class

    putting
    public class node {
    int x;

    public static di[] obj=new di[1];

    obj[0]=new di(22); ---------------------> from here


    public node(int p)
    {
    x=p;
    obj[0]=new di(22); -----------------> to here remove the error : why is this???????????????????????????????????????????


    }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: calling objects in a differnt class

    How are you compiling your program? The output you posted looks like an execution trace, not something from the javac command.

    Your code must have some serious syntax errors. Look at the lines referred to in the error message.

    NOTE: When you post your code, please put it in code tags.
    See:BB Code List - Java Forums
    The code you posted is very hard to read.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: calling objects in a differnt class

    obj[0]=new di(22); ---------------------> from here

    That line of code must be in a method.

  13. The Following User Says Thank You to Norm For This Useful Post:

    jack_nutt (June 18th, 2011)

  14. #13
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: calling objects in a differnt class

    Yeah You can call any functions or methods from any class. For that you have to make object of called class in calling class. By this you can pass any parameters to that class and also call functions or methods from that class. Or you can also make inheritance for that.

Similar Threads

  1. Calling method from .class file
    By alexx_88 in forum Java Theory & Questions
    Replies: 6
    Last Post: April 24th, 2012, 02:14 AM
  2. about calling sub class
    By pokuri in forum Object Oriented Programming
    Replies: 3
    Last Post: January 11th, 2011, 03:30 PM
  3. Calling the actionlistener class depending on variables
    By zidsal in forum Java Theory & Questions
    Replies: 3
    Last Post: January 5th, 2011, 08:52 AM
  4. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 10:24 AM
  5. [SOLVED] How to call string in another class in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2009, 09:31 AM