Belajar Java [OOP] - 13 - Static atau Class Attributes
Summary
TLDRIn this tutorial, the concept of Static variables, also known as class variables, in Java is explored. The presenter demonstrates how Static variables are shared across all objects of a class, meaning they retain the same value for every instance. This session covers how Static variables differ from instance variables, the use of Static methods, and how to manipulate Static values both inside and outside the class. The video explains practical examples using a `Display` class, showcasing how the Static variable impacts all instances of the class when modified. Ideal for beginners to understand Static attributes in object-oriented programming.
Takeaways
- 😀 Static variables (also known as class variables) are shared across all instances of a class.
- 😀 A static variable is associated with the class itself, not individual objects.
- 😀 Instance variables are specific to each object and can vary between instances, unlike static variables.
- 😀 Static variables can be accessed using the class name, making them useful for shared data across instances.
- 😀 In the example, the `display` class had a static variable `type` that was shared between all instances.
- 😀 Static variables are useful for storing values that should remain consistent across all objects of a class.
- 😀 You can modify static variables from within any object, and all other objects will reflect the change.
- 😀 To change the value of a static variable, you can use the class name directly (e.g., `Display.type = 'NewType';`).
- 😀 Encapsulation in Java involves making variables private, which can then be accessed or modified via getter and setter methods.
- 😀 A good practice is to avoid using the `this` keyword with static variables, and instead, use the class name to modify them.
- 😀 The tutorial emphasizes that static variables belong to the class and not to any specific object, ensuring their consistency across all instances.
Q & A
What is a static variable in Java?
-A static variable in Java is a class-level variable that is shared among all instances of that class. It is declared using the 'static' keyword, and its value remains the same across all instances of the class.
How does a static variable differ from a non-static variable?
-A static variable belongs to the class itself and is shared by all objects of the class, while a non-static variable (instance variable) is specific to each object, and its value can differ between instances.
What does the 'static' keyword do in a class?
-The 'static' keyword makes a variable or method part of the class rather than part of any specific object. This means it is shared by all instances of the class, and its state is common across those instances.
What happens if you change a static variable in one instance of a class?
-Since static variables are shared by all instances of the class, changing the value of a static variable in one instance will affect all other instances, as they all reference the same variable.
What is the purpose of the 'this' keyword in Java?
-'this' refers to the current instance of the class. It is used to distinguish between instance variables and parameters or to call other methods in the same object.
How do you access a static variable in Java?
-A static variable can be accessed using the class name, without the need to create an instance of the class. For example, 'ClassName.staticVariable'. It can also be accessed through an object, but it's not recommended.
What is the main difference between static and non-static variables in terms of memory?
-Static variables are stored in the method area of memory and are shared by all instances of the class. Non-static variables, on the other hand, are stored in the heap memory for each individual object, and each object has its own copy.
Can static variables be changed from outside the class?
-Yes, static variables can be changed from outside the class if they are accessible. Typically, they are made public or accessed through getter and setter methods. However, it is recommended to keep them private and use proper encapsulation.
What is an example of when to use a static variable?
-A static variable is useful when you want to keep a value constant across all instances of a class, such as a counter that tracks the number of objects created or a configuration setting that should be shared by all instances.
What happens when you assign a static variable in a subclass?
-When you assign a value to a static variable in a subclass, it affects the value of the static variable for all instances of the class, including those in the parent class, because the static variable is shared across the entire class hierarchy.
Outlines

هذا القسم متوفر فقط للمشتركين. يرجى الترقية للوصول إلى هذه الميزة.
قم بالترقية الآنMindmap

هذا القسم متوفر فقط للمشتركين. يرجى الترقية للوصول إلى هذه الميزة.
قم بالترقية الآنKeywords

هذا القسم متوفر فقط للمشتركين. يرجى الترقية للوصول إلى هذه الميزة.
قم بالترقية الآنHighlights

هذا القسم متوفر فقط للمشتركين. يرجى الترقية للوصول إلى هذه الميزة.
قم بالترقية الآنTranscripts

هذا القسم متوفر فقط للمشتركين. يرجى الترقية للوصول إلى هذه الميزة.
قم بالترقية الآن5.0 / 5 (0 votes)