Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Using static or shared members

From: Foundations of Programming: Object-Oriented Design

Video: Using static or shared members

Up 'til now we've been focused on the instance members of our classes where every object we instantiate will have its own unique copy of all the attributes and all the behaviors we've defined. But we can also create what are called static or shared members, meaning a variable or a method that is shared across all objects in that class. Here's an example. So I have a straightforward SavingsAccount class with an accountNumber attribute, and a balance attribute, deposit and withdraw methods.

Using static or shared members

Up 'til now we've been focused on the instance members of our classes where every object we instantiate will have its own unique copy of all the attributes and all the behaviors we've defined. But we can also create what are called static or shared members, meaning a variable or a method that is shared across all objects in that class. Here's an example. So I have a straightforward SavingsAccount class with an accountNumber attribute, and a balance attribute, deposit and withdraw methods.

And we can instantiate multiple objects based on this class that all have their own copies of all the attributes and behaviors, the variables and methods. But then we realize they all need an interest rate, and that's a pretty straightforward piece of information, and it belongs here. Should savings account have an interested rate? Sure, it should. So we could define that as a regular attribute, an instance variable, and now every instance, every object has its own copy of interest rate. But our business rules say that interest rate is one rate across all savings account, it does not vary from account to account.

It might change ten times a day, but when it changes, it changes for everyone. So there's no need to have potentially thousands of copies of this variable and worry about keeping them all perfectly synchronized. That could be done, but it's not efficient, and it's unnecessary. Now, if you come from a procedural programming background, your gut instinct might be to fix this by pulling out this variable and putting it in some global area by itself. But that's a bad idea when object- oriented programming, and you should be avoiding any generic global dumping ground for data.

And more to the point, the interestRate should be defined in the SavingsAccounts class, the savings account has an interest rate. It belongs here. So we define this as a static or shared variable. This is also referred to as a class-level variable as opposed to an instance-level variable, and all these terms mean the same thing, one copy shared across all objects, static, shared, or class-level. As just a basic example of how this is expressed in code if I'm in a language like C# or Java, instead of creating the variable the normal way with just the type and the name, we just use the word static in there.

If I'm using a language like VB. NET, they use the term Shared. Ruby prefers to call them class level variables, and they use the two At signs to denote those. But they're all referring to the same concept. Now, when I use the term static here, it does not mean constant or fixed. This variable can still change, but there's only one copy across all objects. And if it seems like static is the wrong word for this, know that it doesn't mean static as an unchanging, it means static as opposed to dynamic, because if I use the word static, we now know that there's always one of these variables and only one of these variables, whereas anytime we run the application, there could be one or 10,000 or none of the instance-level variables because that can change depending on how many objects the program creates that time around.

When we access normal instance-level variables, we use the name of the object to get to them. So, if I wanted to access one of the pieces of each of these instances, I would make sure to use the name of the actual object. But if I want to access a static or shared variable, I access it using the class name itself, not any particular instance name. And that's the only way to get to these variables. Now, because it's accessed using the class name, it doesn't matter if you have one object, 10,000 objects, or even no objects at all.

Even if you haven't instantiated any objects from this class yet, you can still use any variables defined as static. So we could set the interest rate for this class before creating any objects of this class. We can also create static methods, methods that exist at the class level, not at any instance level. Again, we've seen how to create a static variable, we'd use the word Static or Shared or the two At signs or however else your language might implement this. And the same way, I would create a normal method, but I use the word Static to actually say that there's only one of these, and it's always accessible using the class name, not the name of any instance of this class.

Now, static methods can only access static variables, static data. I can't write code in a static method to access instance-level variables. But an example here might be that I create static methods to get and set the interest rate, and then I could make the actual currently public static variable interestRate private, the way it should be. Being static does not affect our choices with visibility. And now we could use the name of the class, and then the name of a class-level method, a static method to set the interest rate and control exactly how that happens.

Now, in UML static members of a class are typically shown with an underline, so static attributes, static behaviors. That's how you recognize that these exist at the class level, not the instance level. Now, most classes you define will be composed of mainly instance-level members, but class-level, static or shared members, whatever term you prefer to use, they are a useful and a classic concept in object-oriented design and programming.

Show transcript

This video is part of

Image for Foundations of Programming: Object-Oriented Design
Foundations of Programming: Object-Oriented Design

47 video lessons · 49388 viewers

Simon Allardice
Author

 
Expand all | Collapse all
  1. 11m 35s
    1. Welcome
      1m 25s
    2. Who this course is for
      1m 15s
    3. What to expect from this course
      3m 6s
    4. Exploring object-oriented analysis, design, and development
      1m 41s
    5. Reviewing software development methodologies
      4m 8s
  2. 26m 14s
    1. Why we use object-orientation
      2m 42s
    2. What is an object?
      5m 22s
    3. What is a class?
      4m 43s
    4. What is abstraction?
      2m 45s
    5. What is encapsulation?
      3m 45s
    6. What is inheritance?
      3m 35s
    7. What is polymorphism?
      3m 22s
  3. 12m 16s
    1. Understanding the object-oriented analysis and design processes
      4m 13s
    2. Defining requirements
      6m 9s
    3. Introduction to the Unified Modeling Language (UML)
      1m 54s
  4. 23m 35s
    1. Understanding use cases
      6m 11s
    2. Identifying the actors
      4m 16s
    3. Identifying the scenarios
      5m 7s
    4. Diagramming use cases
      4m 18s
    5. Employing user stories
      3m 43s
  5. 16m 36s
    1. Creating a conceptual model
      1m 59s
    2. Identifying the classes
      2m 27s
    3. Identifying class relationships
      2m 38s
    4. Identifying class responsibilities
      6m 43s
    5. Using CRC cards
      2m 49s
  6. 22m 25s
    1. Creating class diagrams
      6m 11s
    2. Converting class diagrams to code
      4m 57s
    3. Exploring object lifetime
      5m 55s
    4. Using static or shared members
      5m 22s
  7. 19m 49s
    1. Identifying inheritance situations
      6m 49s
    2. Using inheritance
      2m 43s
    3. Using abstract classes
      2m 2s
    4. Using interfaces
      4m 20s
    5. Using aggregation and composition
      3m 55s
  8. 9m 23s
    1. Creating sequence diagrams
      5m 18s
    2. Working with advanced UML diagrams
      2m 3s
    3. Using UML tools
      2m 2s
  9. 10m 39s
    1. Introduction to design patterns
      2m 40s
    2. Example: the singleton pattern
      4m 53s
    3. Example: the memento pattern
      3m 6s
  10. 21m 47s
    1. Introduction to object-oriented design principles
      2m 50s
    2. Exploring general development principles
      3m 55s
    3. Introduction to SOLID principles
      6m 43s
    4. Introduction to GRASP principles
      8m 19s
  11. 7m 1s
    1. Reviewing feature support across different object-oriented languages
      3m 50s
    2. Additional resources
      2m 27s
    3. Goodbye
      44s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Foundations of Programming: Object-Oriented Design.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.