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

  • Home
  • SEARCH
  • 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 6534023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:12:01+00:00 2026-05-25T10:12:01+00:00

The top voted answer to this SA question ( Objective C Static Class Level

  • 0

The top voted answer to this SA question ( Objective C Static Class Level variables ) outlines my question quite well but to it, I’d like to add one more criteria:

Issue Description

  1. You want your ClassA to have a ClassB class variable.
  2. You are using Objective-C as programming language.
  3. Objective-C does not support class variables as C++ does.

  4. I want to access ClassA’s class variable from subclass ClassASub

or even better

4a. I want ClassA’s method to access the class variable as it is, overridden in ClassASub

Any ideas? Or is this just bending Objective-C one step too far?

  • 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-25T10:12:02+00:00Added an answer on May 25, 2026 at 10:12 am

    Just make a normal getter method for your class variable, and you can override it in the subclass. Just remember to access it through the method.

    static SomeClass *gClassVar;
    
    @implementation ClassA
    
    + (SomeClass *)classVar {
        if (!gClassVar)
            gClassVar = ...;
        return gClassVar;
    }
    
    + (...)someMethod {
        [[self classVar] doSomething];
    }
    
    @end
    

    Then,

    static SomeClass *gClassVar;
    
    @implementation ClassASubclass
    
    + (SomeClass *)classVar {
        if (!gClassVar)
            gClassVar = ...;
        return gClassVar;
    }
    
    @end
    

    So, when you call [ClassA someMethod], it will operate on the ClassA instance of classVar. When you call [ClassASubclass someMethod], it will operate on the ClassASubclass instance.

    The idea of having variables of any sort attached to an object (class or instance) is a feature that is kind of “stapled on” to Objective C. Any time you want to do anything object-oriented using Objective C, start by working with methods. (Almost) everything else is just syntactic sugar for things you can do with methods.

    The concept of private / protected / public is somewhat alien to Objective C, even though access control is supported for member variables. The best you can do for methods is to define them in a separate header (and this applies to class variables and properties, if we implement both using methods).

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

Sidebar

Related Questions

The current top-voted to this question states: Another one that's not so much a
The top of my web.xml file looks like this: <?xml version=1.0 encoding=UTF-8?> <web-app xmlns=http://java.sun.com/xml/ns/j2ee
I'm writing a managed cloud stack (on top of hardware-level cloud providers like EC2),
I suspect this answer is in SO but I cannot find it. Some reports
I've used the top voted answer from here to check if a cell value
On top of a dual quad core physical host, I would like to run
Using top it's easy to identify processes that are hogging memory and cpu, but
I know it's a frequent question but I just can't figure it out and
This is in relation to this other SO question which asks how to overwrite
Hopefully this will be the last question I need to ask about this..lol.. 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.