Results 1 to 7 of 7
  1. #1
    rajpan06 is offline Member
    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    Question The following code showing run time error . . . . I want to know the reason

    class OtherClass{
    OtherClass o1=new OtherClass();
    String s="rajeev";
    }
    public class OtherClassObjectAccess {
    public static void main(String[] args) {
    OtherClass o2 = new OtherClass();
    System.out.println("name:"+o2.o1.s);
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,385
    Blog Entries
    7
    Rep Power
    20

    Default Re: The following code showing run time error . . . . I want to know the reason

    If you want to create an OtherClass object, it wants to create an OtherClass object too; but that object wants to create another OtherClass object etc. etc. The stack will overflow or you'll run out of heap memory, whatever comes first.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    rajpan06 is offline Member
    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    Default Re: The following code showing run time error . . . . I want to know the reason

    Then how can i access string variable 's' of class "OtherClass" with respect to object of class "OtherClassObjectAccess" Thanks for your response
    Last edited by rajpan06; 05-25-2013 at 02:02 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,385
    Blog Entries
    7
    Rep Power
    20

    Default Re: The following code showing run time error . . . . I want to know the reason

    Don't create an OtherClass object in the constructor of the OtherClass clas itself; make it create one on demand in a public method. You can call that method whenever you want.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    rajpan06 is offline Member
    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    Default Re: The following code showing run time error . . . . I want to know the reason

    Actually what you are i am not getting it . . . Please modify the code thanks for response

  6. #6
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: The following code showing run time error . . . . I want to know the reason

    Modified :
    class OtherClass{
    String s="rajeev";
    String getName()
    {
    return s;
    }
    }
    public class OtherClassObjectAccess {
    public static void main(String[] args) {
    OtherClass o2 = new OtherClass();
    System.out.println("name:"+o2.getName());
    }
    }
    use this .. I hope it will help..
    if your are not making S private ..you can access it without getter function ..like System.out.println("name:"+o2.s);

  7. #7
    rajpan06 is offline Member
    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    Default Re: The following code showing run time error . . . . I want to know the reason

    Thanks i got it.

Similar Threads

  1. Replies: 3
    Last Post: 09-20-2012, 11:11 AM
  2. Replies: 8
    Last Post: 03-11-2012, 04:28 PM
  3. Replies: 1
    Last Post: 01-31-2012, 08:48 AM
  4. Code stops for no reason
    By Bagzli in forum New To Java
    Replies: 9
    Last Post: 03-03-2011, 03:56 AM
  5. [SOLVED] Can't pinpoint reason for this error..
    By Reiyn in forum New To Java
    Replies: 6
    Last Post: 03-30-2009, 06:39 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
  •