This is my first year as a member of society who has just studied Java. In the comments of the following article I wrote yesterday, the words "** access modifier " and " class design **" came up, so I am writing this article to study and summarize them. https://qiita.com/N46_myHearter/items/891b660b8748171779b1
We are looking for advice on Java.
It can be specified for classes, methods, and variables, and it seems that there are various effects depending on the specified modifier. I only knew about access modifiers such as public private when it was a modifier in Java, but there are a lot of modifiers lol
I knew about access modifiers, abstract, static, final, but what are transient, volatile, synchronized, native, strictfp ....! ?? LOL
There are public, protected, and private, and it seems to indicate where the class and its variables and methods that are designing access modifiers can be accessed.
The accessible range is like public> protected> private.
Class methods that have abstract set are called abstract class methods. .. .. (The abstraction I studied yesterday came out right away ...) If the parent class has abstract set, its child class must be overridden.
** Abstract method ** A method that has no body and consists only of method names, arguments, and return values.
** Abstract class ** A class that contains abstract methods. You will not be able to instantiate and you will need to create an inherited subclass.
If static is set, it can be accessed without instantiation (I think you can understand it because it was static even in C language ... lol). Can be specified for variables and methods. It cannot be overridden. It must be accessible by class name.member name.
** Instantiation ** Make the class accessible and available (Is it initializing the class !?).
When final is set, it seems to prohibit overriding members, overriding in subclasses, inheritance in extends clause, etc. It can be specified for classes, variables, and methods, and the meaning changes depending on what is specified.
** If specified in class ** The specified class cannot be inherited.
** When specified as a variable ** The specified variable is a constant (initialization is required when the variable is declared, and after that, the variable The value cannot be changed).
** When specified in method ** The specified method cannot be overridden.
override Redefining what is defined in the parent class in the child class (like inheritance).
If transient is set, it seems to be excluded from serialization (what is serialization ... lol). Can be specified as a variable.
** Serialize ** Converting an object to a byte array.
If volatile is set, it seems to exclude it from the cache. Can be specified as a variable.
If synchronized is set, the method will be executed synchronously. Can be specified in the method.
** Synchronous execution ** When there are multiple calls at almost the same time, give priority to the one who called first, and let the one who called later wait until the processing is completed.
If native is set, it means that it is implemented in a language other than Java (maybe you can use another language in your Java program). Can be specified in the method.
If strictfp is set, it seems that it is possible to handle floating point strictly. Can be specified for a class or method.
It's just a note that Java beginners have studied, so the content is thin lol Since there are many modifiers, I'm thinking of studying more from access modifiers first (others will be postponed ww).
Recommended Posts