Static Variables and Methods
The static modifier is used to create variables and methods that will exist independently of any instances created for the class. In other words, static members exist before you ever make a new instance of a class, and there will be only one copy of the static member regardless of the number of instances of that class. In other words, all instances of a given class share the same value for any given static variable. We'll cover static members in great detail in the next chapter.
Things you can mark as static:
The static modifier is used to create variables and methods that will exist independently of any instances created for the class. In other words, static members exist before you ever make a new instance of a class, and there will be only one copy of the static member regardless of the number of instances of that class. In other words, all instances of a given class share the same value for any given static variable. We'll cover static members in great detail in the next chapter.
Things you can mark as static:
- Methods
- Variables
- A class nested within another class, but not within a method (more on this in Chapter 8).
- Initialization blocks
Things you can't mark as static:
- Constructors (makes no sense; a constructor is used only to create instances)
- Classes (unless they are nested)
- Interfaces
- Method local inner classes (we'll explore this in Chapter 8)
- Inner class methods and instance variables
- Local variables
- They are not tied to any particular instance of a class.
- No classes instances are needed in order to use static members of the class.
- There is only one copy of a static variable / class and all instances share it.
- static methods do not have direct access to non-static members.
static metotlar override oluna bilməzlər, yenidən elan oluna bilərlər. Bu səbəblə obyektin tipi deyil referans dəyişəninin tipi nəzərə alınır
Comments
Post a Comment