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 3354986
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:20:26+00:00 2026-05-18T02:20:26+00:00

I want to access an object which is created in some method of a

  • 0

I want to access an object which is created in some method of a class, from other method of same class.

public class SomeClass implements SomeInterface{

public void someMethod(int a, int b){ //Implemented method
{
 SomeOtherClass soc = new SomeOtherClass();
//some setting process
}

public int someOtherMethod(){
SomeOtherClass newSOC = soc;//Here, can i access the soc object created in "someMethod"
}
}

Actually, i want to set some data in the object and retrieve the data from different method. Is that possible!!!

  • 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-18T02:20:27+00:00Added an answer on May 18, 2026 at 2:20 am

    Given the constraints that the object instanced of SomeClass is used for multiple requests (e.g. where one request are both invocations of the two methods) and that they must be encapsulated properly, e.g. in a multi user system, you need to have a state object where you can transfer the results of the first method to the second method.

    Since the object instance of SomeClass is considered a shared class in this scenario, you need something else.

    Two ways:

    • Introduce a state object which you can pass from the first method to the second method (e.g. make the first method return the object and pass it as a parameter to the second method)
    • Introduce an internal state which is not visible to other callers. This may be risky if you do not know what you are doing and it heavily depends on the context in which the object is being used.

    Both options require to change the implementation of the first method.

    If you cannot, for any reason, change the implementation of the first method, the answer to your question is: No, you cannot access the soc object from with the second method, as the first object is out of scope, e.g. it may already be garbage collected and gone.

    For the latter, you may want to use a ThreadLocal. This comes in handy if you know that a request (for example a HTTP Servlet Request) is unique for a user or a request and so you can share the information/state object in a ThreadLocal between the invocation of the first method and the second method. If you object instance of SomeClass is reused by different Threads, then you need to take care of properly cleaning up!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to access a integer and a string from a class to all
i want to access or get the class which is present in different project.How
I want to add elements to arrays within an object that I access like
I want to update a Object in another thread, and then access it in
I want to access a MySQL database and I want to read+write data from+to
I have some data I'm getting from a datasource which is a bunch of
I am trying to access properties of an object (person's firstName) which is stored
I want to access a repository using both GIT and SVN clients. A way
I want to access the clipboard using Python 3.1. I've obviously come across win32clipboard,
I want to access the value of a view controller data member variables in

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.