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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:51:37+00:00 2026-05-17T01:51:37+00:00

Normally if I had Class 1 and I wanted to use it elsewhere, I

  • 0

Normally if I had Class 1 and I wanted to use it elsewhere, I would write Class1 *class1 = [[Class1 alloc] init]; to create a new instance. But what if I needed to reference variables in Class1 in another class and did not want to create a new instance, but use an existing one. How would I do that? If I call init, and create a new instance, then I will reset all variables back to 0. I understand this is very elementary, but maybe it is something I never really understood. Any help is appreciated.

  • 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-17T01:51:37+00:00Added an answer on May 17, 2026 at 1:51 am

    Once you have an instance of a class, you can store it in a variable, pass it to other methods, and use it there:

    Class1 *object1 = [[Class1 alloc] init];
    [someOtherObject doSomethingWith:object1];
    

    …

    @implementation SomeOtherClass
    - (void)doSomethingWith:(Class1 *)anObject {
        [anObject blah];
    }
    @end
    

    Addendum

    In other languages, you would want a “class variable”. Objective-C doesn’t have them, but you can simulate them using a static variable:

    static NSString *foo;
    
    @implementation ClassWithSharedFoo
    
    + (NSString *)sharedFoo {
        return foo;
    }
    
    + (void)setSharedFoo:(NSString *)newFoo {
        if(newFoo != foo) {
            [foo release];
            foo = [newFoo retain];
        }
    }
    
    @end
    

    Note that the methods defined here begin with a + instead of a -. This makes them class methods; that is, you send the message to the class itself, not any instance of it:

    [ClassWithSharedFoo setSharedFoo:@"Foo"];
    NSString *foo = [ClassWithSharedFoo sharedFoo];
    

    In C, a static variable is like a global variable, but it is only visible within the file that it was declared in; so, it is shared by all instances of ClassWithSharedFoo, but can’t be accessed or modified elsewhere except by the provided class methods.

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

Sidebar

Related Questions

I had to implement a C function in an Objective-C class that normally would
Normally I would go: bgwExportGrid.RunWorkerCompleted += ReportManager.RunWorkerCompleted; The ReportManager class is a static class
I normally use Firefox and have had no problems with the admin page on
Normally, this code works completely fine, but I have a single instance on a
XNA doesn't have any methods which support circle drawing. Normally when I had to
I am using CPropertySheet class for my design in MFC application, normally in CPropertySheet
I had this code @Local interface IRepo { //... } @Stateless class Repo implements
I have a factory method in a normally instantiable class. Intellisense shows that it
I know this is normally rather stupid, but don't shoot me before reading the
I had a trouble exporting UTF-8 data to Excel, but now it's ok, because

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.