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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:18:07+00:00 2026-06-11T10:18:07+00:00

In a class, I have 2 methods. In method1 I created an object, but

  • 0

In a class, I have 2 methods. In method1 I created an object, but I can’t use the same object in method2.

Why? Please help with a simple example.

The coding is too big, so I have given the layout

public class Sub
{
}

public class DataLoader
{
    public void process1()
    {
        Sub obj = new Sub();
    }

    public void process2()
    {
        // here I can't use the object
    }
}
  • 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-06-11T10:18:08+00:00Added an answer on June 11, 2026 at 10:18 am

    The reason why this isn’t working is scope. A local variable can only be accessed from the block it is declared in. To access it from multiple methods, add a field or pass it to the other method as a parameter.

    Field:

    class YourClass
    {
        object yourObject;
    
        void Method1()
        {
            yourObject = new object();
        }
    
        void Method2()
        {
            int x = yourObject.GetHashCode();
        }
    }
    

    Parameter:

    class YourClass
    {
        void Method1()
        {
            Method2(new object());
        }
    
        void Method2(object theObject)
        {
            int x = theObject.GetHashCode();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with like 50 methods. class MyProgram(object): def method1(self): pass def
I have a convenience class for encoding URI's. In it I've created three methods
I have two ClassLoaders which loads the same class. So, obviously these can't cast
I want to create a Javascript class/object that allow me to have various method:
I have created a class library called AddServiceLibrary in which I have a method
I have a class that creates a calendar. One of the methods finds the
Currently I have created a ABCFactory class that has a single method creating ABC
I have created a gem that adds a new method to the class ActiveRecord::Base.
I have created a class which extends Gallery. There is no onCreate() method in
On the class level, I have created reference: System::Data::Odbc::OdbcConnection Connection; in some method I

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.