- 1. Modifiers
- Public, protected, private, default
- Abstract, static, final,
- Abstraction: Abstract classes, abstract methods
- Static variables, static methods, static code blocks
- Final decorated class, method, variable
- Transient, violatile, native, synchronized
- 2. Variables
- instance variables, local variables, class variables
1. Modifiers
· Public, protected, private, default
Access permissions when modifying a class
|
Current class |
Same package |
Descendant class |
Other Packages |
Public |
√ |
√ |
√ |
√ |
Protected |
√ |
√ |
√ |
X |
Private |
√ |
√ |
X |
X |
default/Default |
√ |
X |
X |
X |
A position that can be modified
|
Class |
Method |
Variable |
Public |
√ |
√ |
√ |
Protected |
X |
√ |
√ |
Private |
X |
√ |
√ |
default/Default |
√ |
√ |
√ |
public class A{}class b{}
abstract ,static, final, transient, violatile, synchronized
General: The position can be modified (there is no such use in the space)
|
Class |
Method |
Variable |
code block |
Abstract |
√ |
√ |
|
|
Static |
|
√ |
√ |
√ |
Final |
√ |
√ |
√ |
|
Transient |
|
|
√ |
|
Native |
|
√ |
|
|
Violatile |
|
|
√ |
|
Synchronized |
√ |
√ |
|
√ |
Abstraction-Abstract classes, abstract methods
/**1. The class that is modified by the abstract is an abstraction class; 2. The permission modifier for the abstract class is public, and the default (as follows) is public;3. An abstract class cannot create an object (that is, it cannot be new); 4. The rest is the same as the normal class. */abstract class Animal {int num;//member variable can use any access modifier (public, protected, private)/** 1. The abstract method must be public or protected, The default is public;2. The abstract method has no method body, that is, the symbol {};3. You must implement an abstract method in a subclass, or the subclass is still an abstract class. */abstract void run (); }class Lion extends animal{void run () {}}
Static variables, static methods, static code blocks
public class Test {public static int a = 100; static variable public int b = 100; //instance variable//static method public static void Method_a () {System.out.println ("===static method===");} instance method public void Method_b () {System.out.println ("===instance method===");} public static void Method_c () {int c = 0;//local variable a = 5;//accessible [static variable]method_a ();//callable [static method]//b = 5; Cannot access [instance variable]//method_b (); Cannot invoke [instance method]}public static void Main (string[] args) {Test t = new Test (); Create an instantiated object int static_var = TEST.A; [Class name. Static variable name] Access int instance_var = t.b; [object. Variable name] access test.method_a (); [Class name. static method Name] Access t.method_b (); [Object. Method name] Access} static { //static block, automatically executes content in block when class loads int d = 1;int c = 0;//local variable a = 5;//accessible [static variable]method_a ();//callable [static method]//b = 5; Cannot access [instance variable]//method_b (); Cannot invoke [instance method]}}
Final
1. The final modified class cannot be inherited;
2. The final decorated method cannot be overridden in a subclass (override)
3. Final modifier variable: basic type variable, value cannot be changed after initialization, reference type variable, cannot point to another object after initialization.
4. Final static modifiers are constants
Transient
public class Test {int A; persistent transient int b; Non-persistent}
Synchronized for thread content, described later in blog post;
Violatile, native rarely contact, and then another description.
2. Variables
According to the location of the declaration: member variables, local variables, class variables
Based on the type of data you belong to: base data type, reference data type
|
Instance variables (member variables) |
Local variables |
class variables (static variables) |
Modifier |
|
Modifier Not present |
Must have static |
Define Location |
Class, methods, construction methods, and statement blocks |
Declared in methods, construction methods, statement blocks |
Class, methods, construction methods, and statement blocks The value of a variable can be specified at the time of declaration, can also be specified in a constructor method, and can be initialized in a static statement block. |
Access |
Object invocation |
|
Class name. Variable name |
Visible |
Instance variables are visible to method/constructor/statement blocks in a class. In general, you should set the instance variable to private. You can make instance variables child classes visible by using the access modifier |
Related methods, construction methods, or block of statements |
has similar visibility to instance variables. However, in order to be visible to the consumer of a class, most static variables are declared as public types |
Survival time |
And the object of survival |
Method/Construction Method/Statement block is created when executed, and destroyed after execution is completed |
Created at the beginning of the program and destroyed at the end of the program. |
Assigning locations |
Heaps (heap) |
Stacks (Stack) |
Static storage (method area) |
Default value |
Homogeneous variables |
No default value, must be initialized to use |
The default value for numeric variables is 0, the Boolean default is False, and the reference type default value is null. |
"Java" Basics: Common modifiers (permission modifiers and abstract, static, final, etc.) and descriptions of variables