zuai-logo
zuai-logo
  1. AP Computer Science A
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion Bank

Why use static variables?

To store values that are common to all instances of a class, like constants or counters.

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

Why use static variables?

To store values that are common to all instances of a class, like constants or counters.

Why use static methods?

To perform operations that don't depend on the state of a specific object.

Can static methods access instance variables?

No, static methods cannot directly access or modify instance variables.

How do you call a static method?

Using the class name followed by the dot operator and the method name (e.g., ClassName.staticMethod()).

What is the relationship between objects and static variables?

All objects of a class share the same static variable; changing it in one object changes it for all.

What is the 'this' reference?

A reference to the current object; static methods do not have a 'this' reference.

What is the significance of static final variables?

They represent constants that are shared across all instances of the class and cannot be changed.

How does using static members improve memory usage?

Static variables are stored only once per class, not once per object, saving memory.

Explain the concept of class-level scope for static variables.

Static variables have class-level scope, meaning they are accessible throughout the class, regardless of instance.

What is the difference between static and non-static methods?

Static methods belong to the class, non-static methods belong to the instance.

Give an example of a real-world use case for static variables.

Counting the number of instances of a class created.

Give an example of a real-world use case for static methods.

Utility methods in a Math class, like calculating square roots or trigonometric functions.

How can static variables be used in game development?

To store game-wide settings or high scores.

How are static methods used in the Singleton design pattern?

A static method provides global access to the single instance of the class.

How are static variables used to manage resources?

They can track the number of active connections to a database.

Describe a scenario where a static method is used for data validation.

A static method can validate input data format without needing an object instance.

How are static variables used in configuration management?

To store default configuration settings that apply to all instances.

How are static methods used for logging?

A static method can write log messages to a file without needing an object instance.

How are static variables used to store application version information?

A static variable can store the version number of the application.

How are static methods used in unit testing?

Static methods can be used to set up test environments or perform utility functions.

What is a static variable?

A variable shared by all instances of a class; belongs to the class itself.

What is a static method?

A method that belongs to the class rather than any specific object of the class.

What does the static keyword indicate?

That a variable or method belongs to the class itself, not an instance of the class.

What is an instance variable?

A variable that is unique to each instance (object) of a class.

What is the dot operator used for with static members?

Accessing static variables and methods using the class name.

What is the purpose of final keyword?

The final keyword is used to declare a constant variable whose value cannot be changed after it is initialized.

What is a class variable?

Another name for a static variable, emphasizing that it belongs to the class.

What is scope in programming?

The region of a program where a defined variable can exist and be recognized.

Define 'getter' method.

A method used to retrieve the value of a private variable.

Define 'setter' method.

A method used to set the value of a private variable.