Java modifiers (access control modifier (default, private, public and protected) and non-access modifiers (static, final, abstract, volatile, etc.))

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_38358499/article/details/98497023

Access control modifiers

  • defult: By default, nothing to write, visible, do not use any modifier in the same package, the default access modifier. Using objects: classes, interfaces, variables, methods
  • private: visible within the same class. Use objects: variables and methods. Access to declare a private variable of type class can only be accessed by the public getter method by external class for data protection and the implementation details of the class behind class
  • public: visible to all classes. Using objects: classes, interfaces, variables, methods. Java program main () method must be provided to the public
  • protected: visible to all classes and subclasses in the same package. Use objects: variables and methods. Parent class declared as protected in the subclass method can be declared as protected, can be declared public, can not be declared private

Non-access modifier

  • static: only a flag used to modify variables and class methods, there are static modification, it shows that the modified members belong to the class, not static modification, indicating that it belongs to the instance, static members (Field,, method, initialization block), and based perish. Statement independent object static method (data obtained from the parameter list, and then calculates the data); statement independent object static variables (also referred to as a static class variable variable, only one copy). Note: Local variables can not be declared as static variables
    //static修饰符用来创建方法和类变量
    public class InstanceCounter
    {
    	private static int numInstances = 0;
    
    	protected static int getCount(){
    		return numInstances;
    	}
    	private static void addInstance(){
    		numInstances++;
    	}
    
    	InstanceCounter(){
    		InstanceCounter.addInstance();
    	}
    
    	public static void main(String[] arguments){
    		System.out.println("starting with " + InstanceCounter.getCount() +  " instance");
    		for(int i = 0; i < 500; ++i){
    			new InstanceCounter();
    		}
    		System.out.println("Created " + InstanceCounter.getCount() + " instances");
    	}
    
    }

    public class StaticTest {
    	//定义一个类Field,它与StaticTest类本身gongcunw
    	static int count = 20;
    	public static void main(String[] args)
    	{
    		//之定义了一个st引用变量,但为指向任何有效的对象
    		StaticTest st = null;
    		//对于静态field、static方法,我们建议将实例可以直接代替成为类名
    		System.out.println(StaticTest.count);
    	}
    }

    Note: You must have a class, then there are objects, static class members and perish. static members are not allowed access to non-static members, this reference, super are not allowed in a static method

  • Final: class for modifying, for modifying variables: member variables (class variables and instance variables) and the local variable (local variable in the method, the local variables of code blocks), a method for modifying. final modified class can not be inherited, a modified method of the class can not be inherited redefined, the modified variables constant, can not be modified, in order to prevent the contents of the process are modified, normally used with the static modifier to create the class constant. Note: specify the default values ​​when defining member variables (final int a = 6;) or (final static double d; static {d = 5.6;}), then the content can not be defined; Final method is overridden by modifying the private access permissions to
    //final修饰实例变量
    public class FinalInstanceVarTest {
    	//final修饰的实例变量必须指定初始值,只能在3个地方指定初始化,并且只能有一个
    	//1.定义时指定初始值
    	//final int age = 10;
    	
    	//2.初始化块
    	/*
    	final int age;
    	{
    		age = 10;
    	}
    	*/
    	
    	//3.构造器方法
    	final int age;
    	{
    		System.out.println("~~~进入初始化块~~~");
    	}
    	FinalInstanceVarTest()
    	{
    		//构造其中可为final实例变量赋值,普通方法不允许为final实例变量赋值
    		age = 45;
    	}
    	
    	public static void main(String[] args)
    	{
    		FinalInstanceVarTest ft = new FinalInstanceVarTest();
    		System.out.println(ft.age);
    	}
    }
    //final修饰类变量
    public class FinalStaticVarTest {
    	//如果final修饰类变量,可以在2个地方为final类变量指定初始值,最多只能指定一次初始值
    	//普通类方法、普通方法、普通初始化块都不能为final实例变量赋值
    	//1.定义时指定初始值
    	//final static int age = 20;
    	
    	//2.静态初始化块
    	final static int age;
    	static{
    		 age = 20;
    	}
    	
    	public static void main(String[] args)
    	{
    		System.out.println(FinalStaticVarTest.age);
    	}
    }

     

  • abstract: modifier for creating an abstract class (of the classes for future expansion, a class can not be modified abstract and final) and abstract method (s implemented method without any particular implementation of the method provided by the sub-classes, can not be declared as final and static)
  • volatile: the member variable each time it is accessed threads, forced to re-read the value of the member variable from shared memory, when a member variable changes, a thread is forced to change the value written back to the shared memory. A volatile object reference may be null
  • synchronize: can be applied to four access modifiers, keyword method declared at the same time can only be accessed by a thread
  • transient: The modifier is included in the statement defines variables, data types for pre-processing and class variables

Guess you like

Origin blog.csdn.net/qq_38358499/article/details/98497023