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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:07:46+00:00 2026-05-17T23:07:46+00:00

I’m working through examples in the book ‘Visual Quick Start, Objective-C’ by Holzner. I

  • 0

I’m working through examples in the book ‘Visual Quick Start, Objective-C’ by Holzner. I spend a lot of time with each example, getting the code debugged is the faster part, and then stepping through saying to myself why each line of code works, what each word in each line does and deciding why the author used one way of doing things versus another. Then I repeat the example with some story of my own. This seems to be a good way to move from being a structured programmer and to an oop-like one. It works with these examples because he just does one concept at a time. (I’ve worked part way through 2 other books and this idea did not work for me in those. Once I got confused by something, I just stayed confused. There were too many variables in the lengthier, more complex examples.)

In the current example (page 137), Holzner uses the word ‘static’. I looked through examples in this book to decide what this word means. I also read the description in Bjarne Stroustrups’ C++ Programming Language book (I understand that C++ and Objective-C are not exactly the same)

(Bjarne Stroustup p 145)
use a static variable as a memory,
instead of a global variable that ‘might be accessed and corrupted by other functions’

Here is what I understand ‘static’ means as a result. I thought that meant that the value of a static variable would never change. I thought that meant it was like a constant value, that once you set it to 1 or 5 it couldn’t be changed during that run.

But in this example piece of code, the value of the static variable does change. So I am really unclear on what ‘static’ means.

(Please ignore the ‘followup question’ I left commented in. I didn’t want to change anything from my run, and risk creating a reading error

Thank you for any clues you can give me. I hope I didn’t put too much detail into this question.

Laurel

…..

Program loaded.
run
[Switching to process 2769]
Running…
The class count is 1
The class count is 2

Debugger stopped.
Program exited with status value:0.

…..

//
//  main.m
//  Using Constructors with Inheritance
//Quick Start Objective C page 137
//

#include <stdio.h>

#include <Foundation/Foundation.h>

@interface TheClass : NSObject

// FOLLOWUP QUESTION - IN last version of contructors we did ivars like this
//{
//  int number;
//}

// Here no curly braces. I THINK because here we are using 'static' and/or maybe cuz keyword?
//   or because in original we had methods and here we are just using inheirted methods

//  And static is more long-lasting retention 'wise than the other method
//             * * * 

// Reminder on use of 'static' (Bjarne Stroustup p 145)
// use a static variable as a memory, 
//     instead of a global variable that 'might be accessed and corrupted by other functions'

static int count;

// remember that the + is a 'class method' that I can execute 
//         using just the class name, no object required (p 84. Quick Start, Holzner)

// So defining a class method 'getCount'

+(int) getCount;


@end

@implementation TheClass

-(TheClass*) init
{
    self = [super init];
    count++;
    return self;
}

+(int) getCount
{ 
    return count;
}

@end


// but since 'count' is static, how can it change it's value? It doeeessss....


int main (void) {
    TheClass *tc1 =  [TheClass new]  ;
    printf("The class count is %i\n", [TheClass getCount]);

    TheClass *tc2 =  [TheClass new]  ;
    printf("The class count is %i\n", [TheClass getCount]);


    return 0;
}
  • 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-17T23:07:46+00:00Added an answer on May 17, 2026 at 11:07 pm

    To clarify No one in particular‘s answer even further, a variable that is static will remain the same throughout all instances of objects, between method calls, etc.

    For instance, declaring the following method:

    - (int)getNumber {
        static int number = 0;
        return ++number;
    }
    

    will return 1, 2, 3, 4, etc., across all instances of the given class at any given time. Neat, eh?

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

Sidebar

Related Questions

No related questions found

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.