Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7510257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:10:35+00:00 2026-05-29T23:10:35+00:00

If i have a class that creates a new object and passes a refrence

  • 0

If i have a class that creates a new object and passes a refrence to this() to that new object, should i just use this() or should i create a declaration to that class and pass the declaration instead. I cant seem to find a clear example of when i would use each case when passing a handle to a child object.

public class ThisClass {

    ThisClass decHandle;
    ThisClass someThing = this;
    public String decString = "some string";
    SomeOtherClass classObject1 = new SomeOtherClass(this);
    SomeOtherClass classObject2 = new SomeOtherClass(decHandle);
    SomeOtherClass classObject3 = new SomeOtherClass(someThing);

}

public class SomeOtherClass {

    ThisClass decHandle;
    public SomeOtherClass (ThisClass handle){
        decHandle = handle; 
    }
    String valueForOther = decHandle.someThing;

}

I mostly need to know which is the best practice when passing handles to a child class from the parent.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:10:37+00:00Added an answer on May 29, 2026 at 11:10 pm

    There are quite a few mistakes here.

    1) The last statement in SomeOtherClass should be

    String valueForOther = decHandle.decString;
    

    instead of

    String valueForOther = decHandle.someThing;
    

    2) When you say

    SomeOtherClass classObject1 = new SomeOtherClass(this);
    

    it will throw a NullPointerException, because you have not yet constructed the object. “this” refers to the current object. In your code, there is no object, therefore this line will throw an exception. The same applies to the next line.

    3) “Child” means that the class inherits from a parent. Just because SomeOtherClass has a reference to ThisClass, it doesn’t mean that SomeOtherClass is a child.

    4) It’s not a good practice to let a class’ members have public visibility.

    Ok, so here’s how you would do it.

    public class ThisClass 
    {
        private String decString = "some string";
        private SomeOtherClass someOtherClass;
    
        public ThisClass()
        {
            someOtherClass = new SomeOtherClass(this);
        }
    
        public String getDecString()
        {
            return decString;   
        }
    }
    
    public class SomeOtherClass 
    {
    
        private ThisClass decHandle;
        private String valueForOther;
    
        public SomeOtherClass (ThisClass handle)
        {
            decHandle = handle;
            valueForOther = decHandle.getDecString(); 
        }  
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: I have an class that inherits from UIImageView. An object creates an instance
I have the following code that creates a serverside object of the xmlhttp class.
I have a class that creates several IDisposable objects, all of these objects are
I have a PHP class that creates a PNG image on the fly and
I have a class Page that creates an instance of DB , which is
I have a utility class that creates & returns a db connection: Public Shared
I have a class that I can have many instances of. Inside it creates
Greetings I have a program that creates multiples instances of a class, runs the
I have a custom thread pool class, that creates some threads that each wait
I have written an apex class that will create a PDF quote and attach

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.